This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: locale::global and wcout


On 7/3/07, Kristian Spangsege <kristian.spangsege@gmail.com> wrote:
> first, a slightly pedantic but important observation: there is only
> *one* standard in force, the ISO / IEC Standard 14882: 2003, available
> in pdf from ISO or in printed form from Wiley: anything else, in
> particular online documents or manuals / books are certainly useful
> while learning and studying the C++ language but do not represent by
> themselves a standard in any sense.
>
> That said, in the area of locale vs streams, a lot is implementation
> defined: our implementation, by defaults has the predefined streams
> (cin, cout, wcin, wcout...) synced with stdio. That means that certainly
> the wchar_t versions are sensible to the global locale, because they use
> directly the underlying wchar_t C library functions to achieve that
> sync. You can change that behavior (thus loosing the sync) by calling
> sync_with_stdio(false) before any I / O.
>
> Paolo.
>

Thanks, for pointing that about the standard out :-) - you are right of cource!

Your answer mostly makes sence to me, but if wcout uses the C library
functions then locale:global(...) should affect it even when wcout is
used before setting the global locale. That does not happen! Here is
the code:

wcout << 1 << endl;
locale::global(locale("en_US.UTF-8"));
wcout << L"\xF8" << endl;

It wants to output a LATIN SMALL LETTER O WITH STROKE, but instead it
outputs '?'. If I remove the first line, it works.

As far as I can see, it should work in both cases if it was the locale
of the C library that was effectively used by wcout.

Do you have an explanation?

:-)

Kristian


I should add that calling wcout.sync_with_stdio(false) does not change the fact that locale::global(...) affects wcout. The code:

locale::global(locale("en_US.UTF-8"));
wcout.sync_with_stdio(false);
wcout << L"\xF8" << endl;

This version outputs a LATIN SMALL LETTER O WITH STROKE. If I remove
the first line then nothing is output. So, it appears that indeed the
C functions are no longer used but the global locale still affects
wcout.

I guess the answer would be that this behavior is legal from the point
of view of the standard. And it is indeed in agreement with BS's
comment if one thinks of wcout as a stream that is created at the time
of the first usage.

What say you?

:-)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]