Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Create C++ std::locale without changing C locale

Reply
Thread Tools

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

 
 
dertopper@web.de
Guest
Posts: n/a
 
      08-26-2008
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
 
Reply With Quote
 
 
 
 
dertopper@web.de
Guest
Posts: n/a
 
      08-26-2008
On 26 Aug., 13:09, Sam <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > Hello newsgroup,

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

>
> 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
 
Reply With Quote
 
 
 
 
Jon
Guest
Posts: n/a
 
      08-26-2008
On Aug 26, 5:21*am, (E-Mail Removed) wrote:
> On 26 Aug., 13:09, Sam <(E-Mail Removed)> wrote:
>
> > (E-Mail Removed) writes:
> > > Hello newsgroup,

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

>
> > 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
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-26-2008
In article <c66cd8b9-2503-4579-894d-
http://www.velocityreviews.com/forums/(E-Mail Removed)>, (E-Mail Removed) says...

[ ... ]

> #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

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
dertopper@web.de
Guest
Posts: n/a
 
      08-26-2008
On 26 Aug., 15:08, Jerry Coffin <(E-Mail Removed)> wrote:
> In article <c66cd8b9-2503-4579-894d-
> (E-Mail Removed)>, (E-Mail Removed) says...
>
> [ ... ]
>
> > #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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Module locale throws exception: unsupported locale setting Sibylle Koczian Python 2 11-20-2010 12:52 PM
the relation between C++ locale and C locale zade C++ 1 03-05-2010 06:04 PM
Re: List of locale values for locale.setlocale() under Windows Gabriel Genellina Python 0 02-18-2009 12:00 AM
i18n problem, involving Locale.getDisplayLanguage and Locale.getDisplayCountry Maurice Hulsman Java 1 07-25-2004 06:11 PM
locale.nl_langinfo(RADIXCHAR) vs locale.localeconv()['decimal_point'] Jeff Epler Python 2 08-31-2003 02:18 PM



Advertisments