Re: wostream &operator <<(wostream &, string const &)?
"Kristof Zelechovski" wrote:
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).
I still do not see the need for such a complication. I can
send primitive types to both specializations, with the
exception of wchar_t [], which makes std::wcout more universal
and preferable.
Whether you chose wcout or cout should depend more on the types
of data you wish to handle internally. It's more of an anomalie
that you can send a char* (pointing to a char[]) to a wcout.
One normally wouldn't expect it to work at all, but doubtlessly,
there are issues concerning interfaces with legacy code and/or
the C library (e.g. the return value of strerror).
But then, all of a sudden, the std::string says 'No way'. If
std::string is meant to be a replacement for char [], it
should behave the same way with respect to std::wcout.
It's meant to be a replacement for char[] in new code. It
certainly doesn't interface directly to legacy code, the C
library, or e.g. Posix interfaces. I don't think that you're
meant to output a char[] to a wcout in new code; logically,
wcout wouldn't even support it, but as I said, there is the
problem of libraries using a C ABI. Obviously, said problem
doesn't concern std::string, so the hack isn't present for
std::string.
--
James Kanze GABI Software
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 ]