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
( 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

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>

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

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



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