Boost logo

Boost Users :

From: Johan RÃ¥de (rade_at_[hidden])
Date: 2006-07-26 06:12:30


袁晓辉 wrote:
> Dear boost-users:
>
> I am really sorry for asking shch a silly question, but I really need
> your help!
>
> boost::lexical_cast is a great function to do casting. If I want is to
> cast from string to a "long long" type value, I can use the follow:
>
> long long llVar = lexical_cast<long long>("123456789");
>
> It is alright in this circs, but what if:
>
> lexical_cast<long
> long>("9999999999999999999999999999999999999999999999999999999999999999999999999999");
>
>
> This is an obvious overflow, but I have no way to handle this error, it
> returns a wrong number without throwing an exception!
>
> Any suggestion? Thank you very much!!!
> Kevin
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

I tested your example with VC++ 7.1, and it throws a bad_lexical_cast.

The problem you have has nothing to do with lexical_cast.
lexical_cast uses streams to do the conversion.
It seems that you use an implementation of the C++ standard library
with poorly implemented streams.

I see two options:

1. Write your own conversion routine by hand. (Not that hard.)

2. Get a better implementation of the C++ standard library.

You can not use atoi, for its behaviour is unspecified when the
conversion can not be carried out.

--Johan RÃ¥de


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net