Re: std::locale question
On Mar 4, 11:09 am, Bart van Ingen Schenau <b...@ingen.ddns.info>
wrote:
Brad Tilley Wrote:
I'm experimenting with std::locale. This code works:
int main()
{
// Get the run time locale.
const std::locale rt_locale( std::locale("") );
// Make the run time locale global.
std::locale::global( rt_locale );
// Print a few non-English Characters
std::wcout << L"\x2660 \x2661 \x2662 \x2663" <<
std::endl;
return 0;
}
However, adding this line breaks it:
// Print the run time locale
std::cout << rt_locale.name() << std::endl;
Here is working output:
$ ./a.out
=E2=99 =E2=99=A1 =E2=99=A2 =E2=99=A3
Here is broken output:
$ ./a.out
en_US.utf8
` a b c
I don't understand why .name() would cause this. I've never
worked with std::locale before, so I'm probably making some
fundamental mistake. Any advice is appreciated. Here's the g++
version I'm using:
g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Your problem does not lie with the use of locale::name, but with using
std::cout to print it.
std::cout and std::wcout use the same underlying stream
(stdout) to produce their output.
They shouldn't. They're both synchronized with stdio, but
they're separate objects, with their own streambuf. And it is
the streambuf which should be handling any character
translation. (Of course, the standard doesn't require the
standard streams to have any particular type of streambuf, and
the character translation is defined by the derived streambuf
type, so I guess anything the implementation does is legal.)
The problem is that stdout can not handle a mixture of narrow
output (from cout) and wide output (from wcout). The first use
of a stream determines its orientation (narrow or wide) and it
keeps that orientation for the rest of its lifetime, or until
it is explicitly changed by the user (with a call to fwide).
That's true for stdio, but not for iostream.
There are restrictions with regards to changing encodings, I
think; once you've used a multibyte encoding, or maybe it's a
non-trivial encoding, you can't change. But you can read or
write a certain number of characters in "C" locale, then change
encodings. (This is necessary if you want to read XML, for
example.)
The best option is not to mix cout and wcout in a program.
The second-best option is to set the orientation of stdout
explicitly when changing between cout and wcout (or vice
versa).
Changes made in stdout do not, or at least should not, in a
standard conforming implementation, affect cout and wcout.
--
James Kanze