473,398 Members | 2,404 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,398 software developers and data experts.

Problem using locales

Can anyone explain way the transformation to upper case doesn't work
correctly in the following code if PROBLEM is defined but works correctly if
it's not defined? I'm using VC 7.1
#include <locale>
#include <string>
#include <algorithm>

#define PROBLEM

struct ToUpper

{
ToUpper(std::locale const& locale) : mLocale(locale) {}

char operator() (char c) const { return std::toupper(c,mLocale); }

std::locale const& mLocale;
};
#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif

std::string s("abc едц");

std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );

return 0;
}
Feb 11 '06 #1
4 2539
* B.D.:
Can anyone explain way the transformation to upper case doesn't work
correctly in the following code if PROBLEM is defined but works correctly if
it's not defined? I'm using VC 7.1

#include <locale>
#include <string>
#include <algorithm>
#define PROBLEM

struct ToUpper
{
ToUpper(std::locale const& locale) : mLocale(locale) {}
char operator() (char c) const { return std::toupper(c,mLocale); }
std::locale const& mLocale;
};

#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif
std::string s("abc едц");
std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );
return 0;
}


I could reproduce your problem with Visual C++ 7.1, where if PROBLEM is
defined (global instance of 'my_local') the local name is reported as
"Swedish_Sweden.1252", but the transformed string as "ABC едц".

The problem could not be reproduced using g++ 3.4.4, since that compiler
doesn't support the "Swedish" locale, at least not without doing
something special.

Note: the standard only mandates support for the "C" and "" locales, and
is self-contradictory on what other locale names can be (only
standard C locale names, whatever they are, or implementation defined
names).

So really the code should have a try-catch.

The problem with Visual C++ 7.1 likely to be a library implementation
bug due to order-of-initialization of globals.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 11 '06 #2
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program transforms the characters in the
string s to upper case. The last three strange characters in the string are national characters
that are part of the Swedish alphabet. What I don't understand is way the program behaves
differently if PROBLEM is defined. What I real would like to know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is more general than it first
appears? Should I expected this type of behavior when I instantiate other classes as well? If the
problem is related to how or global objects are initializing, as suggested by Jim Langston
(alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++) then perhaps the problem is a bit
more general than I first expected?

"B.D." <bd@algonet.se> wrote in message news:ki*******************@newsb.telia.net...
Can anyone explain way the transformation to upper case doesn't work correctly in the following
code if PROBLEM is defined but works correctly if it's not defined? I'm using VC 7.1
#include <locale>
#include <string>
#include <algorithm>

#define PROBLEM

struct ToUpper

{
ToUpper(std::locale const& locale) : mLocale(locale) {}

char operator() (char c) const { return std::toupper(c,mLocale); }

std::locale const& mLocale;
};
#ifdef PROBLEM
std::locale my_locale( "Swedish" );
#endif

int main ()
{
#ifndef PROBLEM
std::locale my_locale( "Swedish" );
#endif

std::string s("abc едц");

std::transform( s.begin(), s.end(), s.begin(), ToUpper(my_locale) );

return 0;
}


Feb 12 '06 #3
TB
B.D. sade:
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program
transforms the characters in the string s to upper case. The last three
strange characters in the string are national characters that are part
of the Swedish alphabet. What I don't understand is way the program
behaves differently if PROBLEM is defined. What I real would like to
know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is
more general than it first appears? Should I expected this type of
behavior when I instantiate other classes as well? If the problem is
related to how or global objects are initializing, as suggested by Jim
Langston (alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++)
then perhaps the problem is a bit more general than I first expected?


The problem is most likely with VC++. There are no problems with the code's
expected behaviour using Borland's C++ Compiler with RW or STLport.

--
TB @ SWEDEN
Feb 12 '06 #4
Ok, many thanks to you for helping clarify and verify this.

- B.D
"TB" <TB@SWEDEN> wrote in message news:43**********************@taz.nntpserver.com.. .
B.D. sade:
Thanks the responses and suggestions.

I apologize if I was a bit vague in my initial posting. The program
transforms the characters in the string s to upper case. The last three
strange characters in the string are national characters that are part
of the Swedish alphabet. What I don't understand is way the program
behaves differently if PROBLEM is defined. What I real would like to
know if this is a:

a) programmer issue (more education is needed, perhaps by reading a
good book that cover this topic)

b) compiler issue

c) C++ language issue

Another thing that would be interesting to know is if this behavior is
more general than it first appears? Should I expected this type of
behavior when I instantiate other classes as well? If the problem is
related to how or global objects are initializing, as suggested by Jim
Langston (alt.comp.langlearn.c-c++) and Alf P. Steinbach (comp.lang.c++)
then perhaps the problem is a bit more general than I first expected?


The problem is most likely with VC++. There are no problems with the code's
expected behaviour using Borland's C++ Compiler with RW or STLport.

--
TB @ SWEDEN

Feb 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: garykpdx | last post by:
Is there some sort of tutorial on locales or the locale module? I can't seem to find a list showing all possible locales. I made 'en' work alright, but when I tried 'de' or 'de_DE' or 'es_ES',...
0
by: David List | last post by:
I seem to have a problem installing facets into locales explicitly. It is not clear to me whether the problem is my compiler, my environment or my code. I am trying out some code from...
6
by: Gustav Lead | last post by:
Hi all, I have trouble reading back floating point data from a file. It appears as if the tokens for positive and negative infinity (1.#INF and -1.#INF) that my implementation writes get...
1
by: John Smith | last post by:
Hi, I hope somebody can help here. I have an app. that uses both MS Access 2000 and SQL Server databases running under XP and 2003 server. The problem I have encountered during testing was...
2
by: corley | last post by:
Hello world, I am having a problem with the decimal symbol. The result of writing a floating-point-number to an xml document (using libxml2 from xmlsoft.org) is <number>10,1234</number> ...
0
by: | last post by:
Hi all, running the following code as an console application: #include <locale> #include <iostream> #include <sstream> #include <tchar.h> using namespace std;
8
by: Rik | last post by:
Hi all, is there a in PHP to get the available locales without shellacces (i.e. locale -a)? Grtz, -- Rik Wasmus
2
by: cris | last post by:
Hi, I got trouble when I try to change the locale used in a program foo.cc // ----------------- begin #include <iostream> #include <locale> #include <exception> using namespace std; int...
4
by: Gilles Ganault | last post by:
Hello What does it take to support locales? The following test code found on the PHP site doesn't work as planned: ======= setlocale(LC_TIME, "C"); echo strftime("%A"); setlocale(LC_TIME,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...
0
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...

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.