473,757 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

? Bug in libstdc++? GCC 4.1: wcout.imbue(loc ) should (?) set stream encoder's, but it doesn't; std::locale::gl obal() 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::gl obal(loc) ; // (2)
// std::wcout.imbu e(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::gl obal(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 3895
(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.imbu e(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.imbu e(loc) ;

than you got -
terminate called after throwing an instance of
'std::ios_base: :failure'
what(): basic_filebuf:: _M_convert_to_e xternal 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::gl obal(loc) ;
// std::wcout << L" !" << std::endl ; // will cause
exception
std::wcout.imbu e(loc) ;
std::wcout << L" !" << std::endl ; // now OK
std::locale::gl obal(std::local e::classic()) ;
std::wcout << L" !" << std::endl ; // OK, using
stream's loc not global
std::wcout.imbu e(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
7852
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 long int to store the value, and the precision (number of decimal points) is variable (it's a templated class): template <size_t _decimal_places = 4> class FixedFloat {
10
1731
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. How can this be happening?
2
1570
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
1306
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
872
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
3516
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 std::istringstream but fails to do so, could anybody see what is wrong with the code? (Output follows the code.) I fear that I have not completely grasped how the time_getXXX methods should be used in the operator<< ? With best regards,
0
1931
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
9779
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 *not* sufficient to imbue the stream with a locale like "de_DE.UTF-8". Doing so only takes care of facets of decimal numbers and the like. Rather, one has to call locale::global("de_DE.UTF-8"). Is this behaviour conforming to the standard? And if...
44
4379
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
9297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.