Why does std :: cin change the value of its operand, even if it does not work?

I am firmly convinced that this std::basic_istream::operator>>is a "safe transaction", that is, if it fails, it will not change its operand. So, I expected for the following code

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss("a");
    int i = 42;
    ss >> i; // extraction fails
    std::cout << i; // modifies i, WHY?!
}

Live on Coliru

value iwill remain 42. However, starting with C ++ 11, this is no longer the case, and the operand is reset to zero on error. Quote from cppreference.com(underline mine)

If the extraction fails (for example, if a letter was entered where the number is expected), it valueremains unchanged and is failbitset. ( before C ++ 11 )

, value failbit. , , std::numeric_limits<T>::max() std::numeric_limits<T>::min() failbit. (, ++ 11)

- , ? , . -std=c++98, gcc5.3 clang3.6 ++ 11.

+4

Source: https://habr.com/ru/post/1623376/


All Articles