Topic: wostream &operator <<(wostream &, string const &)?
Author: "kanze" <kanze@gabi-soft.fr>
Date: Mon, 21 Aug 2006 05:02:37 CST Raw View
peter koch wrote:
> kuyper@wizard.net wrote:
> > "Kristof Zelechovski" wrote:
> > > Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
> > > news:1155062150.001381.325180@h48g2000cwc.googlegroups.com...
> > > > "K i tof 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.
> This is not necesarily so. You can imagine applications that
> use a mix of char and wchar_t strings (the application I
> currently work on has this). Some parts of the system has to
> access external data that is keyed on 8-bit characters (e.g.
> resource files, databases and so on). Even if the application
> per se is unicode, this will not be the case for the external
> data - ever. So how can I present the data if I can't strem
> it to a wide output stream? I believe it is a reasonable
> expectation that the conversion is handled automatically.
I think that the *intent* was that for programs using Unicode,
internally, everything would be wchar_t, and externally,
everything would be char, with conversion on the fly in
std::filebuf. That's why a std::wfilebuf reads and writes
char's in the file.
How well this works in practice, I don't know. I do know that
there are systems which support IO directly in wchar_t. I also
know that there are applications which use UTF-8 internally.
Which means that you could conceivably end up doing the
opposite: using char's internally, but wchar_t externally.
--
James Kanze GABI Software
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 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 ]
Author: "kanze" <kanze@gabi-soft.fr>
Date: Mon, 21 Aug 2006 09:51:24 CST Raw View
"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 orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 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 ]
Author: giecrilj@stegny.2a.pl ("Kristof Zelechovski")
Date: Fri, 18 Aug 2006 16:13:52 GMT Raw View
> 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. 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.
Chris
---
[ 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 ]
Author: James Kanze <kanze.james@neuf.fr>
Date: Wed, 16 Aug 2006 11:26:23 CST Raw View
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...
>>>> "K i tof 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 orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 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 ]
Author: musiphil@bawi.org (Seungbeom Kim)
Date: Wed, 16 Aug 2006 16:27:26 GMT Raw View
SuperKoko wrote:
> Obvious solution : At a namespace scope in your program:
>
> #ifdef UNICODE /* or something like that*/
> std::ostream& tcout=std::wcout;
> #else
> std::ostream& tcout=std::wcout;
> #endif
>
> Or perhaps
> std::ostream& tcout=(sizeof(_TCHAR)==sizeof(char))?std::cout :
> std::wcout;
std::wcout is of type std::wostream, not std::ostream, therefore it
cannot bind to a reference to std::ostream.
#ifdef UNICODE
std::wostream& tcout = std::wcout;
#else
std::ostream& tcout = std::cout;
#endif
is possibly acceptable.
--
Seungbeom Kim
---
[ 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 ]
Author: giecrilj@stegny.2a.pl ("Kristof Zelechovski")
Date: Wed, 16 Aug 2006 16:27:49 GMT Raw View
Uzytkownik "SuperKoko" <tabkannaz@yahoo.fr> napisal w wiadomosci
news:1155368731.463685.267490@75g2000cwc.googlegroups.com...
> Or perhaps
> std::ostream& tcout=(sizeof(_TCHAR)==sizeof(char))?std::cout :
> std::wcout;
Ill-formed: cannot cast from std::wostream to std::ostream.
Chris
---
[ 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 ]
Author: giecrilj@stegny.2a.pl ("K i tof elechovski")
Date: Tue, 8 Aug 2006 12:03:45 GMT Raw View
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?
I thought about a workaround: it is easy to implement. But I am not sure I
am allowed to do this, which is my question:
std::wostream &operator <<(std::wostream &, std::string const &);
I feel such an operator should be in the standard and my definition is a
patch. The standard forbids patching the standard library, doesn't it?
And also I would like to know how to get rid of the following annoying
problem: the code that sends wchar_t const [] to std::cout compiles and
prints the address of the buffer which is obviously meaningless. Since it
is hard to find all such places in the code, I would prefer if the compiler
could report them to me. Is it possible to set up the environment so as to
catch such dangerous places?
Chris
---
[ 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 ]
Author: "AllanW" <allan_w@my-dejanews.com>
Date: Tue, 8 Aug 2006 14:56:29 CST Raw View
"K i tof 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?
---
[ 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 ]
Author: giecrilj@stegny.2a.pl ("Kristof Zelechovski")
Date: Wed, 9 Aug 2006 11:32:21 GMT Raw View
Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci=20
news:1155062150.001381.325180@h48g2000cwc.googlegroups.com...
> "K=F8i=B9tof =AEelechovski" wrote:
>> I am trying to go Unicode with my code base. I noticed that in most=20
>> cases I
>> can safely replace cout with wcout since all standard types behave
>> correctly. However, all but one. The offender is std::string. I can=
not
>> send it to std::wcout. WHY? If std::string is a replacement for char=
=20
>> const
>> [] and I can send char const [] to std::wcout, why cannot I do the sam=
e=20
>> 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=20
std::string is what I have.
Chris=20
---
[ 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 ]
Author: kuyper@wizard.net
Date: Wed, 9 Aug 2006 10:40:38 CST Raw View
"Kristof Zelechovski" wrote:
> Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
> news:1155062150.001381.325180@h48g2000cwc.googlegroups.com...
> > "K i tof 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.
---
[ 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 ]
Author: "Kristof Zelechovski" <giecrilj@stegny.2a.pl>
Date: Thu, 10 Aug 2006 09:46:37 CST Raw View
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...
>> > "K i tof 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? 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.
I used std::wcout for both Unicode and ANSI builds but I stumbled on the
std::string problem. Therefore my question: I can std::wcout << "", why
cannot I std::wcout << std::string("")? It seems something is missing here.
Chris
---
[ 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 ]
Author: "peter koch" <peter.koch.larsen@gmail.com>
Date: Fri, 11 Aug 2006 13:06:35 CST Raw View
kuyper@wizard.net wrote:
> "Kristof Zelechovski" wrote:
> > Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
> > news:1155062150.001381.325180@h48g2000cwc.googlegroups.com...
> > > "K i tof 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.
>
This is not necesarily so. You can imagine applications that use a mix
of char and wchar_t strings (the application I currently work on has
this). Some parts of the system has to access external data that is
keyed on 8-bit characters (e.g. resource files, databases and so on).
Even if the application per se is unicode, this will not be the case
for the external data - ever.
So how can I present the data if I can't strem it to a wide output
stream? I believe it is a reasonable expectation that the conversion is
handled automatically.
/Peter
---
[ 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 ]
Author: "SuperKoko" <tabkannaz@yahoo.fr>
Date: Tue, 15 Aug 2006 09:01:49 CST Raw View
Kristof Zelechovski wrote:
> 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? 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.
> I used std::wcout for both Unicode and ANSI builds but I stumbled on the
> std::string problem. Therefore my question: I can std::wcout << "", why
> cannot I std::wcout << std::string("")? It seems something is missing here.
>
Obvious solution : At a namespace scope in your program:
#ifdef UNICODE /* or something like that*/
std::ostream& tcout=std::wcout;
#else
std::ostream& tcout=std::wcout;
#endif
Or perhaps
std::ostream& tcout=(sizeof(_TCHAR)==sizeof(char))?std::cout :
std::wcout;
K i tof elechovski wrote:
>
> I thought about a workaround: it is easy to implement. But I am not sure I
> am allowed to do this, which is my question:
> std::wostream &operator <<(std::wostream &, std::string const &);
>
Adding overloads to namespace std is not allowed (and it might
potentially be a name clash problem if the standard adds this exact
overload in C++0x)
However, if your above function is not declared in namespace std
(declared in the global namespace), thanks to koenig lookup, it should
work as expected.
So, basically, I think that your workaround is legal.
---
[ 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 ]