Mixing setlocale() and c++ locales

J

J Trauntvein

I was working with a co-worker the other day to work through the
process of formatting numeric values by imbueing C++ iostreams with
locales. His program's initialisation code had a call to
setlocale("",LC_ALL) which I believe sets up locale information for the
C run time library but is not supposed to effect the C++ run time.
What we found, however, was that, with this function call in place, the
C++ iostreams were formatting floating point values according to the US
convention (commas as a thousands separator and periods as a decimal)
even though the stream was imbued with a different locale (Brasilian
Portuguese in our case). We found that, if we removed the above
mentioned call that the iostream formatting worked aas we expected. My
question is whether this interaction is a bug or is it rather an
unfortunate but planned side effect that comes from mixing the "C"
and C++ locales models?

Regards,

Jon Trauntvein
 
T

Tom Widmer

J said:
I was working with a co-worker the other day to work through the
process of formatting numeric values by imbueing C++ iostreams with
locales. His program's initialisation code had a call to
setlocale("",LC_ALL) which I believe sets up locale information for the
C run time library but is not supposed to effect the C++ run time.
What we found, however, was that, with this function call in place, the
C++ iostreams were formatting floating point values according to the US
convention (commas as a thousands separator and periods as a decimal)
even though the stream was imbued with a different locale (Brasilian
Portuguese in our case). We found that, if we removed the above
mentioned call that the iostream formatting worked aas we expected. My
question is whether this interaction is a bug or is it rather an
unfortunate but planned side effect that comes from mixing the "C"
and C++ locales models?

Do you have a test case? The following prints "1,234" for me on VC7.1:

#include <iostream>
#include <locale>
#include <locale.h>

int main()
{
std::locale loc("Portuguese_Brazil");
std::cout.imbue(loc);
setlocale(LC_ALL, "");
std::cout << 1.234 << '\n';
}

Moving the setlocale call to the start makes no difference. The C and
C++ locale systems are synchronized to a degree. See
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=locale2.html#locale
for some info on this, or check the C++ standard.

Tom
 
J

J Trauntvein

Tom said:
Do you have a test case? The following prints "1,234" for me on VC7.1:

#include <iostream>
#include <locale>
#include <locale.h>

int main()
{
std::locale loc("Portuguese_Brazil");
std::cout.imbue(loc);
setlocale(LC_ALL, "");
std::cout << 1.234 << '\n';
}

Moving the setlocale call to the start makes no difference. The C and
C++ locale systems are synchronized to a degree. See
http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=locale2.html#locale

for some info on this, or check the C++ standard.

Tom



I just tried the following code:

#include <iostream>
#include <locale.h>


int main()
{
double const val = 123456.789;

setlocale(LC_ALL,"");
std::cout.imbue(std::locale("Portuguese_Brazil"));
std::cout << val << std::endl;
return 0;
} // main


This produces the behaviour that I described provided that the
operating system regional settings are set to Brasilian Portuguese. It
does not appear when the regional locale is set to US English.
Commenting out the setlocale() call produces the correct behaviour.

Regards,

Jon Trauntvein
 
T

Tom Widmer

J said:
I just tried the following code:

#include <iostream>
#include <locale.h>


int main()
{
double const val = 123456.789;

setlocale(LC_ALL,"");
std::cout.imbue(std::locale("Portuguese_Brazil"));
std::cout << val << std::endl;
return 0;
} // main


This produces the behaviour that I described provided that the
operating system regional settings are set to Brasilian Portuguese. It
does not appear when the regional locale is set to US English.
Commenting out the setlocale() call produces the correct behaviour.

Ok, I managed to get the same behaviour. Note that changing:
setlocale(LC_ALL,"");
to
std::locale::global(std::locale(""));
gives the same effect without using the C library.

Fiddling around with the regional settings and the code, I didn't work
out the the logic behind the way it works (or, rather, doesn't work).
I'd recommend just removing the call to setlocale if at all possible.
Further, I'd recommend posting in microsoft.public.vc.stl, where
P.J.Plauger and Pete Becker (both of Dinkumware, the standard library
supplier for VC) are likely to see it, and hopefully tell you how it
works, and how to fix your problem.

Tom
 

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

Top