473,219 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

? 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?

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
2 3849
(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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
10
by: Kyle Kolander | last post by:
Got a strange bug going on here... // proper includes and additional code here int x = 2552123; cout << x; This results in output like this: 2,552,123 Same behavior with stringstreams. ...
2
by: Matthias Kluwe | last post by:
Hi! My problem looks like this: #include <iostream> #include <locale> int main(int argc, char* argv) { std::locale::global( std::locale( "deu" ) );
1
by: Matthias Kluwe | last post by:
Hi! My problem looks like this: #include <locale> #include <iostream> int main(int argc, char* argv) { std::locale::global( std::locale( "deu" ) );
0
by: Matthias Kluwe | last post by:
Hi! My problem looks like this: #include <locale> #include <iostream> int main() { std::locale::global( std::locale( "deu" ) ); // or "german"
7
by: Peter Jansson | last post by:
Dear group, I have been struggling to get a simple program for inserting and extracting std::tm objects to/from streams to work. The code below tries to read a std::tm object from a...
0
by: Ralf Goertz | last post by:
Hi, consider the following program loc.cc #include <iostream> #include <fstream> #include <string> #include <locale> using namespace std;
7
by: Ralf Goertz | last post by:
Hi, since my previous post <455440ad$0$30326$9b4e6d93@newsspool1.arcor-online.netis still unanswered I'd like to rephrase my question. In order to read/write a wstring in UTF-8 encoding it is...
44
by: Ioannis Vranos | last post by:
Has anyone actually managed to print non-English text by using wcout or wprintf and the rest of standard, wide character functions?
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.