Boost logo

Boost Users :

From: Terje Slettebų (tslettebo_at_[hidden])
Date: 2003-04-18 15:32:38


>From: "Vladimir Prus" <ghost_at_[hidden]>

>Subject: [boost] lexical_cast problem

Not again. :)

> I've just updated my project to use boost 1.30 and also gcc 3.2 and see
> exception which reads:
>
> bad lexical cast: source type value could not be interpreted as target,
> Target=i, Source=Ss

Ah, yes, good ol' g++ with its wonderfully descriptive type_info names... ;)

> The simple reproduction recipe is:
>
> #include <boost/lexical_cast.hpp>
> #include <string>
>
> int main()
> {
> boost::lexical_cast<int>(std::string(" 0"));
> }

"Houston, we have a problem..."

Hm, just now, I sent a reply to a similar problem report at Boost-Users
list, about why whitespace skipping is turned off in the new lexical_cast
version. It was to avoid having things like "lexical_cast<std::string>(' ');
// Space character" work, which used to throw an exception.

However, it seems this change has had undesired side-effects, in that code
like the above now fails to work.

Frankly, I don't know what to do with this...

> Note the space in string. The exception does not occur with gcc 2.95, but
only
> with 3.2. I wonder if this should/can be fixed? Personally, I'd expect
this
> code to work.
>
> And a related note: the exception's what() above is not quite helpfull.

The current version in the CVS has got a changed exception handling, where
bad_lexical_cast now looks like this:

    class bad_lexical_cast : public std::bad_cast
    {
    public:
        // ...
        const std::type_info &source_type() const
        const std::type_info &target_type() const

        virtual const char *what() const throw()
        {
            return "bad lexical cast: "
                   "source type value could not be interpreted as target";
        }
    };

In other words, what() now returns the original text, and the type_info
objects for the types used is now returned by member functions.

> Is it
> possible to output the stringstream content (if source -> stringstream
> conversion was successfull?)

Yes, that's possible. I guess I'll leave it to Kevlin to comment on this.

Thanks for the report, to both you and Tom Matelich at Boost-Users list.

Regards,

Terje


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