? Bug in libstdc++? GCC 4.1: wcout.imbue(loc) should (?) set stream encoder's, but it doesn't; std::

Y

year1943

There was the same topic not so long ago, but as I see it stays w/o
answer:
http://groups.google.ru/group/comp....&q=locale+imbue&rnum=1&hl=ru#fe109c899f916871

As Bjarne Stroustrup said in his book,
"in Stroustrup (retranslated from German)
"Setting the global locale does not affect existing input/output
streams. The streams continue to use those locales that were assigned
to
them using imbue() during their creation." "

But actually I see quite the opposite behaviour.
The following code:

#include <iostream>
// #include <clocale>
#include <locale>

int main() {
std::locale loc("") ;
// std::setlocale(LC_ALL, "") ; // (1)
std::locale::global(loc) ; // (2)
// std::wcout.imbue(loc) ; // (3)
std::wcout << L"(non-ASCII national letters here)" << std::endl ;

std::wstring wstr ;
std::wcin >> wstr ;
std::wcout << L'[' << wstr << L']' << std::endl ;

return 0 ;
}

DOES work (it correctly inputs and outputs non-ASCII, non-"C"-locale
chars), while it SHOULD NOT (?), and if you commented out (1) and (2)
and uncommented (3) - it DOESN'T work, though it SHOULD DO (as
Stroustrup says).

More over, if you switch global locale between points of wcout usage,
like:

int main() {
std::locale loc("") ;
std::wcout << L"(non-ASCII national letters here)" << std::endl ;
std::locale::global(loc) ; // (2)
std::wcout << L"(non-ASCII national letters here)" << std::endl ;

- wcout DOESN'T WORK correctly even after global locale switch!!!

What's wrong with my attempts to make it work??

(I'm afraid, it's one more of numerous problems/bugs of C/C++
alliance, but would happy to hear "official" answer and ways to
workaround...)
 
Y

year1943

(I'm afraid, it's one more of numerous problems/bugs of C/C++
alliance, but would happy to hear "official" answer and ways to
workaround...)

It seems (partially) right. Call to

std::ios_base::sync_with_stdio(false) ;

at the every beginning changes behaviour, but it still stays looking
buggy or "impl defined":

int main() {
std::ios_base::sync_with_stdio(false) ;
std::locale loc("") ;

std::wcout << L"Hello world!" << std::endl ;
std::wcout.imbue(loc) ;
std::wcout << L" !" << std::endl ;

this works fine, as expected.
If you try to output non-ASCI chars with "C" locale on stream:

std::wcout << L" !" << std::endl ;

nothing is printed at all, even with flushing endl manip; following
even ASCII strings also disappears silently to blackhole;
but if later you call imbue:

std::wcout << L" !" << std::endl ;
std::wcout.imbue(loc) ;

than you got -
terminate called after throwing an instance of
'std::ios_base::failure'
what(): basic_filebuf::_M_convert_to_external conversion error
Aborted

It seems changing the global locale now doesn't affect stream:

int main() {
std::ios_base::sync_with_stdio(false) ;
std::locale loc("") ;

std::wcout << L"Hello world!" << std::endl ;
std::locale::global(loc) ;
// std::wcout << L" !" << std::endl ; // will cause
exception
std::wcout.imbue(loc) ;
std::wcout << L" !" << std::endl ; // now OK
std::locale::global(std::locale::classic()) ;
std::wcout << L" !" << std::endl ; // OK, using
stream's loc not global
std::wcout.imbue(std::locale::classic()) ;
// std::wcout << L" !" << std::endl ; // will cause
exception
std::wcout << L"Hello world!" << std::endl ; // OK


Either Bjarne was inaccurate or at least not pedantic with his locale/
stream descriptions... or libstdc++ is implemented in a such manner?..
 
Y

year1943

Sorry, empty literals above, like
L" !"
are consisting of national non-ASCII chars, now we can't see them due
to buggy unicode support by Google - Goo replaced alphabetic chars
with spaces. :eek:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top