 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Křištof Želechovski Guest
|
Posted: Tue Aug 08, 2006 5:03 pm Post subject: wostream &operator <<(wostream &, string const &)? |
|
|
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 ] |
|
| Back to top |
|
 |
AllanW Guest
|
Posted: Tue Aug 08, 2006 8:56 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
"Křištof Želechovski" wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Kristof Zelechovski Guest
|
Posted: Wed Aug 09, 2006 4:32 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
news:1155062150.001381.325180 (AT) h48g2000cwc (DOT) googlegroups.com...
| Quote: | "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.
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 ] |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Aug 09, 2006 4:40 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
"Kristof Zelechovski" wrote:
| Quote: | Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
news:1155062150.001381.325180 (AT) h48g2000cwc (DOT) 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 ] |
|
| Back to top |
|
 |
Kristof Zelechovski Guest
|
Posted: Thu Aug 10, 2006 3:46 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
Uzytkownik <kuyper (AT) wizard (DOT) net> napisal w wiadomosci
news:1155135995.608597.116130 (AT) i42g2000cwa (DOT) googlegroups.com...
| Quote: | "Kristof Zelechovski" wrote:
Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
news:1155062150.001381.325180 (AT) h48g2000cwc (DOT) 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 ] |
|
| Back to top |
|
 |
peter koch Guest
|
Posted: Fri Aug 11, 2006 7:06 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
kuyper (AT) wizard (DOT) net wrote:
| Quote: | "Kristof Zelechovski" wrote:
Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
news:1155062150.001381.325180 (AT) h48g2000cwc (DOT) 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 ] |
|
| Back to top |
|
 |
SuperKoko Guest
|
Posted: Tue Aug 15, 2006 3:01 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
Kristof Zelechovski wrote:
| Quote: | 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:
| Quote: |
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 ] |
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Wed Aug 16, 2006 5:26 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
Kristof Zelechovski wrote:
| Quote: | Uzytkownik <kuyper (AT) wizard (DOT) net> napisal w wiadomosci
news:1155135995.608597.116130 (AT) i42g2000cwa (DOT) googlegroups.com...
"Kristof Zelechovski" wrote:
Uzytkownik "AllanW" <allan_w@my-dejanews.com> napisal w wiadomosci
news:1155062150.001381.325180 (AT) h48g2000cwc (DOT) 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.
| Quote: | 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 (AT) neuf (DOT) 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 ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Wed Aug 16, 2006 9:27 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
SuperKoko wrote:
| Quote: | 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 ] |
|
| Back to top |
|
 |
Kristof Zelechovski Guest
|
Posted: Wed Aug 16, 2006 9:27 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
Uzytkownik "SuperKoko" <tabkannaz (AT) yahoo (DOT) fr> napisal w wiadomosci
news:1155368731.463685.267490 (AT) 75g2000cwc (DOT) googlegroups.com...
| Quote: | 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 ] |
|
| Back to top |
|
 |
Kristof Zelechovski Guest
|
Posted: Fri Aug 18, 2006 9:13 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
| Quote: | 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 ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Aug 21, 2006 3:51 pm Post subject: Re: wostream &operator <<(wostream &, string const &)? |
|
|
"Kristof Zelechovski" wrote:
| Quote: | 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).
| Quote: | 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 ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|