From: Corwin Joy (cjoy_at_[hidden])
Date: 2001-07-20 19:12:18
----- Original Message -----
From: "Jeff Garland" <jeff_at_[hidden]>
Sent: Friday, July 20, 2001 7:00 PM
Subject: RE: [boost] a lexical_cast<> bug?
> I believe this is the documented (and reasonable) behavior.
I'm not necessarily saying it's unreasonable. My issue was that if this is
seen to be the 'correct' behavior, I think it would be good for the docs to
mention this explicitly since it was fairly non-obvious from reading them
that the int cast would fail.
> You are really
> asking for 2 conversion from string->double and then from double to int
Well, I'm not sure I agree with that. Really, what I want is for it to
stream into an integer & tell me if it cannot do so correctly.
>I would note, however, that the docs speak of "lexical_context"
> that allows control of this behavior. AFAIK the context isn't
> Anyway the doc sez -->
> "Returns the result of streaming arg into a std::stringstream and then out
> Target object. The conversion is parameterized by the current
> 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
> set, otherwise a Target() is returned. "
I saw that, but I didn't think it made it obvious that the int cast would
> 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
> give the right correct result either. I think this has been discussed
Another good point! And perhaps also worth mentioning in the doc!
> > -----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
> > 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
> > 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
> > 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
> > contains a fractional part.
> > Info: http://www.boost.org Unsubscribe:
> > <mailto:boost-unsubscribe_at_[hidden]>
> > Your use of Yahoo! Groups is subject to
> Info: http://www.boost.org Unsubscribe:
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Boost list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk