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

Discussion in 'C++' started by dertopper@web.de, Aug 26, 2008.

  1. Guest

    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
     
    , Aug 26, 2008
    #1
    1. Advertising

  2. Guest

    On 26 Aug., 13:09, Sam <> wrote:
    > 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
     
    , Aug 26, 2008
    #2
    1. Advertising

  3. Jon Guest

    On Aug 26, 5:21 am, wrote:
    > On 26 Aug., 13:09, Sam <> wrote:
    >
    > > 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
     
    Jon, Aug 26, 2008
    #3
  4. Jerry Coffin Guest

    In article <c66cd8b9-2503-4579-894d-
    >, 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.
     
    Jerry Coffin, Aug 26, 2008
    #4
  5. Guest

    On 26 Aug., 15:08, Jerry Coffin <> wrote:
    > In article <c66cd8b9-2503-4579-894d-
    > >, 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.
     
    , Aug 26, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page