So the question is when will the conversion fail? Unit tests check for
the cases between [min(), max()], but what about under/overflows (>max,
<min)? Is it unspecified?
It seems it is currently handled a bit inconsistent:
#include <boost/test/unit_test.hpp>
#include <boost/lexical_cast.hpp>
#include <limits.h>
using std::string;
using std::numeric_limits;
using boost::lexical_cast;
string s;
// warning: needs to be a part of a unit test
//#define min max
#define T short
// add a digit!
s = lexical_cast<string, T>(numeric_limits<T>::min()) + "0";
BOOST_CHECK_THROW(lexical_cast<T>(s), boost::exception); // throws
s = lexical_cast<string, T>(numeric_limits<T>::min()) + "5";
BOOST_CHECK_THROW(lexical_cast<T>(s), boost::exception); // throws
#define T boost::int64_t
s = lexical_cast<string, T>(numeric_limits<T>::min()) + "0";
BOOST_CHECK_THROW(lexical_cast<T>(s), boost::exception); // NO throw!!!
s = lexical_cast<string, T>(numeric_limits<T>::min()) + "5";
BOOST_CHECK_THROW(lexical_cast<T>(s), boost::exception); // throws
The behavior is really weird.
The docs don't mention what should happen.
If I use max instead of min every test throws.
Is there something in boost that can convert a *whole* string to it's
integer representation and throw on under/overflows and errors?
-- Hrvoje Prgeša
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
boost::uint64_t max = numeric_limits<boost::uint64_t>::max();
s = lexical_cast<string, boost::uint64_t>(max);
BOOST_CHECK_EQUAL(max, lexical_cast<boost::uint64_t>(s));
// crashes: std::bad_cast: bad lexical cast: source type value could not
be interpreted as target
Converting 64bit unsigned int to it's max value string representation
and back results in exception.