If I read an integer from a istream using the >> operator, and the represented integer larger than INT_MAX then the operation just returns INT_MAX.
I am currently doing a comparison against INT_MAX to detect the overflow, but if the operation is inputted "2147483647" then it will return an error when in reality there was none and the result is valid.
Example: http://ideone.com/4bXyGd
#include <iostream>
#include <sstream>
#include <climits>
int main() {
std::istringstream st("1234567890123"); // Try with 2147483647
int result;
st >> result;
if (result == INT_MAX)
std::cout << "Overflow!" << std::endl;
else
std::cout << result << std::endl;
return 0;
}
What is the ideologically correct way to do this?