By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,550 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
There was the same topic not so long ago, but as I see it stays w/o
answer:
http://groups.google.ru/group/comp.l...109c899f916871

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...)

Sep 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
(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?..
Sep 11 '07 #2

P: n/a
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. :o)

Sep 11 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.