Problem using locales

B

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;
}
 
A

Alf P. Steinbach

* 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.
 
B

B.D.

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top