For common parsing crashes (including too many or too many), you can simply check the bad bit of the string string to specify. The easiest way to do this:
if (!st) { std::cout << "Could not parse number" << std::endl; }
Before C ++ 11 could not verify the specifics for overflow or overflow using this method. However, in C ++ 11, if the parsed value is too large or too small for the type, the result will be set to the largest value that the type can hold ( std::numeric_limits<Type>::max() or std::numeric_limits<Type>::min() ), in addition to the bit error.
So in C ++ 11, to check if the value was too big or small, you can do:
if (!st) { if (result == std::numeric_limits<int>::max()) { std::cout << "Overflow!" << std::endl; } else if (result == std::numeric_limits<int>::min()) { std::cout << "Underflow!" << std::endl; } else { std::cout << "Some other parse error" << std::endl; } }
source share