locked
Does std::locale::global work? RRS feed

  • Question

  • Hi

    Using VS2008, I can do

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

    using std::cout;
    using std::locale;

    void dumpcat()
    {
            for (int i = LC_MIN; i <= LC_MAX; i++)
            {       const char *q = setlocale(i, NULL);
                    cout << "setlocale(i,NULL)=[" << ((q == NULL) ? "NULL" : q) << "]\n";
            }
            cout << '\n';
    }

    int main()
    {
            dumpcat();

            locale newlocale(std::locale::classic(), ".1252", std::locale::ctype);
            locale::global(newlocale);

            dumpcat();

            return 0;
    }
    >>

    I am expecting this C++ program to change the C locale because global is set.
    On print out, I see

    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]

    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[English_United Kingdom.1252]

    but for 2nd set of 6, since std::locale::ctype was specified, I was expecting to see

    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[English_United Kingdom.1252]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]
    setlocale(i,NULL)=[C]

    Why do I not see this?
    Essentially I want to change just 1 category type for global (and propagate back to C locale).

    Thanks


    IdentityWrapper
    Monday, October 10, 2011 6:02 PM

Answers

  • As far as I am concerned, this is a bug. My expections are that just ctype should be changed, no other category.
    It is no different from

    locale newlocale1(".1252");
    locale newlocale2(locale::classic(), newlocale1, locale::ctype);
    locale::global(newlocale2);

    which should also respect category type. 

    I can't find any code on Microsoft's sites apart from trivial examples and certainly none that involve C++ locales & category types.

    This should be fixed.


    IdentityWrapper
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 7:12 PM
  • Have you searched the Connect site for any reports of
    similar issues?

    http://connect.microsoft.com/VisualStudio

    e.g. - search for setlocale

    Note that you can submit feedback there if you're
    convinced it's an unreported bug.

    - Wayne
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 7:51 PM
  • "ISO/IEC 14882:2003" says:
    22.1.1.5 locale static members [lib.locale.statics]
    static locale global(const locale& loc);

    1 Sets the global locale to its argument.
    2 Effects: Causes future calls to the constructor locale() to return a copy of the argument. If the argument has a name, does
        std::setlocale(LC_ALL, loc.name().c_str());
    otherwise, the effect on the C locale, if any, is implementation-defined. No library function other than locale::global() shall affect the value returned by locale().
    3 Returns: The previous value of locale().
    So I don't think there's a bug. It's the same in the latest C++11 draft.
    gg

    • Edited by Codeplug Monday, October 10, 2011 9:56 PM
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 9:55 PM

All replies

  • As far as I am concerned, this is a bug. My expections are that just ctype should be changed, no other category.
    It is no different from

    locale newlocale1(".1252");
    locale newlocale2(locale::classic(), newlocale1, locale::ctype);
    locale::global(newlocale2);

    which should also respect category type. 

    I can't find any code on Microsoft's sites apart from trivial examples and certainly none that involve C++ locales & category types.

    This should be fixed.


    IdentityWrapper
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 7:12 PM
  • Have you searched the Connect site for any reports of
    similar issues?

    http://connect.microsoft.com/VisualStudio

    e.g. - search for setlocale

    Note that you can submit feedback there if you're
    convinced it's an unreported bug.

    - Wayne
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 7:51 PM
  • "ISO/IEC 14882:2003" says:
    22.1.1.5 locale static members [lib.locale.statics]
    static locale global(const locale& loc);

    1 Sets the global locale to its argument.
    2 Effects: Causes future calls to the constructor locale() to return a copy of the argument. If the argument has a name, does
        std::setlocale(LC_ALL, loc.name().c_str());
    otherwise, the effect on the C locale, if any, is implementation-defined. No library function other than locale::global() shall affect the value returned by locale().
    3 Returns: The previous value of locale().
    So I don't think there's a bug. It's the same in the latest C++11 draft.
    gg

    • Edited by Codeplug Monday, October 10, 2011 9:56 PM
    • Marked as answer by Rob Pan Monday, October 17, 2011 8:48 AM
    Monday, October 10, 2011 9:55 PM