Boost logo

Boost :

From: Jeff Garland (jeff_at_[hidden])
Date: 2001-07-20 19:00:10

I believe this is the documented (and reasonable) behavior. You are really
asking for 2 conversion from string->double and then from double to int with
truncation. I would note, however, that the docs speak of "lexical_context"
that allows control of this behavior. AFAIK the context isn't implemented.
Anyway the doc sez -->

"Returns the result of streaming arg into a std::stringstream and then out as a
Target object. The conversion is parameterized by the current lexical_context,
if set. If the conversion is unsuccessful, a bad_lexical_cast exception is
thrown if the current
lexical_context is set for throwing or if there is no current lexical_context
set, otherwise a Target() is returned. "

The more subtle issue is when the double is truncated due to the default
precision setting of the stream. This doesn't throw an exception, but doesn't
give the right correct result either. I think this has been discussed


> -----Original Message-----
> From: Corwin Joy [mailto:cjoy_at_[hidden]]
> Sent: Friday, July 20, 2001 7:38 PM
> To: boost_at_[hidden]
> Subject: [boost] a lexical_cast<> bug?
> 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.
> Info: Unsubscribe:
> <mailto:boost-unsubscribe_at_[hidden]>
> Your use of Yahoo! Groups is subject to

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