Instead of atoi I use operator>> of stringstream in the following function in order to convert a string into an int.
int MyString::Int()
{
int f = 0;
std::stringstream ss;
ss << *this;
ss >> f;
return f;
}
Is it guarenteed that operator>> does not touch integer variable "f" if it cannot convert the string data coming from "*this" into the integer?
Or can it happen that stringstream "ss" is set to failure by operator<< and therefore operator>> makes undefined behaviour because I do not check the state of the stream?
Edit: The answer is already here: istream behavior change in C++ upon failure There it is described that the behaviour changed in C++11 regarding if the value is left unchanged in some circumstances or set to zero if a conversion cannot be made. In my example above however the int variable is initialised anyway and therefore the return value would always be zero independently if the stream reaches eof before or the conversion is not possible.