Dmitry J Dmitry J - 12 days ago 12
C++ Question

Is it guaranteed that standard extraction operator>> does not change argument in case of failure?

If calling something like

input_stream >> i;
where ``* is of arithmetic type, throws exception or sets badbit etc., is it guaranteed that
i
has not changed?

Answer

Before C++11, the value was left as it was, [reference]:

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. (until C++11)

But after C++11, no. It is set to 0 if extraction fails (same reference):

If extraction fails, zero is written to value and failbit is set. If extraction results in the value too large or too small to fit in value, std::numeric_limits<T>::max() or std::numeric_limits<T>::min() is written and failbit flag is set. (since C++11)