Create C++ std::locale without changing C locale

D

dertopper

Hello newsgroup,

is it possible to create a std::locale object without changing the
global C locale? Is this implementation defined?

My problem is that I work in a multi-threaded environment that has to
deal with two different locales. Unfortunately, the code uses plain C
functions for IO at many places. However, there is a single point that
uses std::isprint with a C++ locale that is different from the
standard locale. That's why I'm interested in quick and dirty solution
to construct a C++ locale without changing the global C locale.

I've found two other possible work-arounds: (A) printf_l. I don't know
whether this is part of the C standard but nevertheless my IO library
doesn't contain it. (B) A Microsoft Visual C specific solution using a
function called _configthreadlocale which is also not part of my IO
library (there is no information available when this function was
added to Visual C).

Any suggestions (even if they are just "replace all C calls by
iostream calls") are highly welcome.

TIA,
Stuart
 
D

dertopper

Although I haven't checked, I'd be surprised if merely instantiating a
std::locale object changes the global C locale.

I would expect that the global C locale would only be affected by
std::locale::global().

Yes, I thought so, too. The following programme taught me else.

#include <xlocale>
int main(int argc, char* argv[])
{
printf ("%f\n", 3.14);
std::locale loc ("German_Germany");
printf ("%f", 3.14);
}

Output:3.140000
3,140000

Is my implementation of the Standard Library (Visual C 6.0) faulty,
then?

Stuart
 
J

Jon

Although I haven't checked, I'd be surprised if merely instantiating a
std::locale object changes the global C locale.
I would expect that the global C locale would only be affected by
std::locale::global().

Yes, I thought so, too. The following programme taught me else.

#include <xlocale>
int main(int argc, char* argv[])
{
  printf ("%f\n", 3.14);
  std::locale loc ("German_Germany");
  printf ("%f", 3.14);

}

Output:3.140000
3,140000

Is my implementation of the Standard Library (Visual C 6.0) faulty,
then?

In a word, yes it is. However, in this particular case, the problem
arises from a misunderstanding. In order to have a C++ locale have
any influence on program output, it must be imbued into a stream as
follows:

std::locale loc("German_Germany");
std::cout.imbue(loc);

The mere creation of a locale will not have any effect and, further,
will not effect the locale state of any of the "C" formatting
functions.

Regards,

Jon Trauntvein
 
J

Jerry Coffin

[ ... ]
#include <xlocale>
int main(int argc, char* argv[])
{
printf ("%f\n", 3.14);
std::locale loc ("German_Germany");
printf ("%f", 3.14);
}

Output:3.140000
3,140000

Is my implementation of the Standard Library (Visual C 6.0) faulty,
then?

Yes. Your code isn't entirely correct either, but fixing the code
doesn't make any difference with VC 6. Here's the corrected code:

#include <locale>

int main(int argc, char* argv[])
{
printf ("%f\n", 3.14);
std::locale loc ("German_Germany");
printf ("%f", 3.14);
}

The output with VC 6 is the same as you showed, but with VC 7 (or newer)
or almost any other recent compiler, the output is like this:

3.140000
3.140000
 
D

dertopper

[ ... ]
#include <xlocale>
int main(int argc, char* argv[])
{
  printf ("%f\n", 3.14);
  std::locale loc ("German_Germany");
  printf ("%f", 3.14);
}
Output:3.140000
3,140000

Is my implementation of the Standard Library (Visual C 6.0) faulty,
then?

Yes. Your code isn't entirely correct either, but fixing the code
doesn't make any difference with VC 6. Here's the corrected code:

#include <locale>

int main(int argc, char* argv[])
{
  printf ("%f\n", 3.14);
  std::locale loc ("German_Germany");
  printf ("%f", 3.14);

}

The output with VC 6 is the same as you showed, but with VC 7 (or newer)
or almost any other recent compiler, the output is like this:

3.140000
3.140000

Thanks.
 

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,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top