Boost logo

Boost Users :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-01-27 12:15:10


Gottlob Frege wrote:
>
>
> On 1/22/07, *Tobias Schwinger* <tschwinger_at_[hidden]
> <mailto:tschwinger_at_[hidden]>> wrote:
>
> John Christopher wrote:
> > Hello,
> > Does Boost contain facilities to convert std::string into
> std::wstring and
> > the other way around?
>
> Should there?
>
> std::string a;
> std::wstring b( a.begin(),a.end());
> std::string c(b.begin(),b.end());
>
> All string algorithms in Boost are templates so they should work fine
> with both std::string and std::wstring.
>
> http://www.boost.org/doc/html/string_algo.html
>
> Regards,
> Tobias
>
>
>
> I think the real question is whether the OP was hoping for Unicode
> conversions of some kind in the process. ie what 'type' (sematically)
> are the wchar_t's and char's? UTF16? UTF32? (what size is the wchar_t?)
> etc.

I'm not sure that's what the OP really wanted:

If we want to convert back and forth we are probably just using the
(compatible) ASCII subset (a typical example: COM calls).

A proper conversion to UTF-8 when narrowing the character type can be
worse than overflow if the rest of the code can't handle UTF-8.

However, Boost.Unicode sure would be a nice addition.

> Also, does the last line:
> std::string c(b.begin(), b.end())
> give at least a compiler warning about downconverting from wchar_t to
> char, and thus losing bits?

Diagnostic messages are implementation-defined. The code I posted does
not warn with gcc4 (all warnings enabled), for instance.

> (Which would be a hint that maybe you need
> some Unicode conversion or something else.)

Yeah, like most compiler warnings, it means "know what you're doing" ;-).

Regards,
Tobias


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