From: Oliver Kullmann (O.Kullmann_at_[hidden])
Date: 2005-10-17 07:10:32
Consider the following basic task: Given an integer variable "n"
and an input stream "in"
read an integer from in and put it into n. The "solution"
in >> n;
is not a solution, since according to 18.104.22.168.2 from the C++ standard
the locale's num_get<> object is invoked, which according to
22.214.171.124.2 from the C++ standard invokes scanf from the C library,
which then according to 126.96.36.199 from the C standard, clause 10,
yields undefined behaviour if the number represented in the stream is not
representable by the type int.
Thus, since "in" represents here user input, and user input shall never
yield undefined behaviour, we cannot use "in >> n;".
It seems that in the boost library there is no tool to remedy the
- Although boost::lexical_cast does not specify which errors are checked
and which are not, it seems that lexical_cast just forwards the action
to some std::stringstream object, and the same problem as above
- Boost::numeric_cast only works with objects of numeric types, and
thus does not contribute to the problem of converting a character
sequence into an integer.
So, it seems, that this fundamental problem is neither addressed by
the C++ standard library nor by the Boost library, and thus I have to
write a parser etc. myself for the arithmetic types in C++ ?
Is this really so??
P.S. The easiest way to parse the input from in and check for overflow
/ underflow seems to me to get the sequence of digits into a
erase leading zeros, and compare this string to the string
std::string ref = boost::lexical_cast<std::string>(std::numerical_limits<int>::max());
In case of overflow: If digits.size() < ref.size(), then fine, if
digits.size() > ref.size(), then bad, otherwise we use lexicographical
A convenient design then perhaps is a wrapper class
template <typename Int>
such that we can use
in >> n;
if (not in)
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk