471,584 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,584 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 2320
* 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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by garykpdx | last post: by
reply views Thread by David List | last post: by
6 posts views Thread by Gustav Lead | last post: by
1 post views Thread by John Smith | last post: by
2 posts views Thread by corley | last post: by
8 posts views Thread by Rik | last post: by
4 posts views Thread by Gilles Ganault | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

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.