Boost logo

Boost :

From: Corwin Joy (cjoy_at_[hidden])
Date: 2001-07-20 18:37:59

While I was working with the lexical_cast program under VC6 I noticed the
following (suprising) behaviour:

string s("123.456");
double d = lexical_cast<double>(s); // this will succeed
int i = lexical_cast<int>(s); // this will throw

It turns out that the lexical_cast to int will throw because the stream
operator into int stops at the decimal point. Therefore lexical cast says
that the int operator did not "succeed" because it did not read the full
string -- even though it reads the integer part of the string and
initializes the local result to 123 just fine. It's a bit of a surprise
from a user point of view that this fails. So the question is:
A. Is it intended that the class should throw in this case?
B. If it is intended / this kind of cast is not allowed it seems to me like
a strong warning in the docs should be added.

IMHO - given the documentation, my preference would be to modify
lexical_cast to support casting of a string to an int even when the string
contains a fractional part.

Boost list run by bdawes at, gregod at, cpdaniel at, john at