If there is a legitimate possibility that the string represents a value that is too large to be stored in int , convert it to something more and check if the result matches in int :
long long temp = stoll(y); if (std::numeric_limits<int>::max() < temp || temp < std::numeric_limits<int>::min()) throw my_invalid_input_exception(); int i = temp;
source share