Re: wostream &operator <<(wostream &, string const &)?
Kristof Zelechovski wrote:
> Uzytkownik <kuyper@wizard.net> napisal w wiadomosci
> news:1155135995.608597.116130@i42g2000cwa.googlegroups.com...
>> "Kristof Zelechovski" wrote:
>>> Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
>>> news:1155062150.001381.325180@h48g2000cwc.googlegroups.com...
>>>> "KHitof ?elechovski" wrote:
>>>>> I am trying to go Unicode with my code base. I noticed that in most
>>>>> cases I
>>>>> can safely replace cout with wcout since all standard types behave
>>>>> correctly. However, all but one. The offender is std::string. I
>>>>> cannot
>>>>> send it to std::wcout. WHY? If std::string is a replacement for
char
>>>>> const
>>>>> [] and I can send char const [] to std::wcout, why cannot I do the
>>>>> same
>>>>> with
>>>>> a std::string?
>>>> Have you tried the same with a std::wstring?
>>> Yes, I have. But what does it buy me? Nothing. Because
>>> std::wcout and std::string is what I have.
>> Well, as long as you have std::string rather than
>> std::wstring, you haven't converted completely to wide
>> characters. And as long as you have a mixture of narrow and
>> wide character types, you're going to have to deal
>> periodically with converting between them.
> In order to convert completely to wide characters, I would
> have to use std::cout for an ANSI build and std::wcout for an
> Unicode build, right?
First, the standard doesn't say anything about Unicode. As far
as the standard is concerned, wchar_t can have the same type and
encoding as char. Typically, it will be larger, however, at
least on general purpose machines, but that still doesn't
guarantee Unicode. And of course, which representation of
Unicode: Unicode defines three encoding forms: UTF-8, UTF-16 and
UTF-32---UTF-16 and UTF-32 exist in big endian and little endian
formats, when used on 8 bit media.
If you are using UTF-16 (Windows, AIX) or UTF-32 (most other
systems) internally, you need to use wchar_t (supposing that
your implementation supports one of these with wchar_t).
Everywhere, which means std::wstring, L"..." for string
literals, and L'...' for character constants, along with the
wide character versions of the iostream classes. If you are
using UTF-8, on the other hand, you will continue using the char
versions (i.e. std::string, std::cout, etc.), but you'd best be
prepared for some extra work if you do any character handling.
> And how do you achieve that? By replacing all references to
> std::cout with a function call? use_cout<_TCHAR>() <<
> std::endl? It does not look particularly well.
Wide characters and narrow characters are two different beasts.
For that matter, UTF-16 and UTF-32 are two different beasts. If
you're doing anything complicated, you'll have to maintain two
different versions. Otherwise, a judicious use of typedefs and
maybe a few macros can help:
// header ansi/characters.hh
namespace characters {
typedef std::string string ;
typedef std::istream istream ;
// ...
std::ostream& cout = std::cout ;
// ...
}
and
// header unicode/characters.hh
namespace characters {
typedef std::wstring string ;
typedef std::wistream istream ;
// ...
std::wostream& cout = std::wcout ;
// ...
}
Include one or the other using -I (or /I, depending on your
compiler).
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S9mard, 78210 St.-Cyr-l'cole, France +33 (0)1 30 23 00 34
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]