473,395 Members | 1,730 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,395 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 3859
(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?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.