Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2002-11-30 07:59:00


>From: "Jasper van de Gronde" <th.v.d.gronde_at_[hidden]>

> I use lexical_cast a lot, but when I tried to use it in a program that
> uses Unicode I noticed it failed because it uses a standard
> stringstream. To solve my problem I simply added another #ifdef that
> checked for UNICODE and would then use wstringstream, thinking it would
> probably be noticed by somebody else and that it would be fixed properly
> in a future release, but it still isn't.

Yes, it's noticed, and it's fixed in a proposition that is scheduled to be
reviewed by the author, Kevlin Henney, before the Boost 1.30 release. It's
found here
(http://groups.yahoo.com/group/boost/files/lexical_cast_proposition/). The
new version is completely generic in that it can use any character type for
the stream.

The character type is deduced from the arguments, so you can use it like
this:

std::string str=lexical_cast<std::string>(1.23);
std::wstring wstr=lexical_cast<std::wstring>(1.23);

or even:

std::basic_string<some_type> str=lexical_cast<std::basic_string<some_type>
>(1.23);

It's also possible to specialise it for various types (by specialising
lexical_cast_impl), although the interface for that will likely change. It
has various speedups, which bypasses the stringstream, when possible, and
it's also planned to incorporate the speedup suggestion by Gennadiy
(http://groups.yahoo.com/group/boost/files/lexical_cast_propositions/).

The proposition also fixes the problem with whitespace in the conversion,
which used to throw exceptions. E.g.:

std::string str=lexical_cast<std::string>(' '); // Used to throw
bad_lexical_cast

Regards,

Terje


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk