c++locale

How to use environment locale except for numbers?


I'd like to combine the environment locale (for toUpper and such) with the C locale (for number formatting) in C++, how would I do that? I've read https://en.cppreference.com/w/cpp/locale/locale/locale.html which seems to indicate one should be able to override facets with facets of other locales (constructor 8), but I don't understand what category cats is here. It did not work with

std::locale base("");
std::locale for_numbers("C");
std::locale mixed(base, for_numbers, std::numpunct<char>::id);

which gives /usr/include/c++/13/bits/locale_classes.h:196:64: note: no known conversion for argument 3 from ‘std::locale::id’ to ‘std::locale::category’ {aka ‘int’}


Solution

  • category is a member typedef of std::locale class. You should use one of the provided constants, in this case std::locale::numeric

    std::locale base("");
    std::locale for_numbers("C");
    std::locale mixed(base, for_numbers, std::locale::numeric);
    

    See it online