Hrvoje Prgeša | 6 Sep 17:13

[lexical_cast] bad_lexical_cast behavior?

 From the documentation:
"If the conversion is unsuccessful, a  bad_lexical_cast exception is 
thrown. "
"Exception used to indicate runtime lexical_cast  failure. "

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

(Continue reading)

Hrvoje Prgeša | 6 Sep 18:00

Re: [lexical_cast] bad_lexical_cast behavior?

And another weirdness:

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.

-- Hrvoje Prgeša

Gmane