std::locale::combine(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::locale::combine(3) πŸ’…

okay so this is the std::locale::combine page from the C++ Standard Libary manual, and it’s genuinely such a neat little locale remix situation, bestie. πŸŽ€

🌸 NAME 🌸

std::locale::combine β€” literally the combine page, no cap. ✨

✨ Synopsis ✨

Defined in header <locale>, and the signature is giving very specific main character energy:

template< class Facet >
locale combine( const locale& other ) const;

okay so what this does: it constructs a locale object that is a copy of *this except for the facet of type Facet, which is copied from other. πŸ¦‹

⚠️ important caveat, bestie: the program is ill-formed if Facet is not a facet or if Facet is a volatile-qualified facet. so do not play with that. πŸ’€

πŸ’– Return value πŸ’–

The new, nameless, locale. That’s the whole vibe: new locale, no name, just facet energy. πŸ‘‘

πŸ”₯ Exceptions πŸ”₯

std::runtime_error if other does not implement Facet. not the moment, honestly. 😭

🌈 Example 🌈

// Run this code
        #include <iostream>
        #include <locale>

        int main()
        {
            const double number = 1000.25;
            std::cout << "\"C\" locale: " << number << '\n';
            std::locale loc = std::locale()
                .combine<std::numpunct<char>>(std::locale("en_US.UTF8"));
            std::cout.imbue(loc);
            std::cout << "\"C\" locale with en_US numpunct: " << number <<
       '\n';
        }

πŸ“ Output πŸ“

        "C" locale: 1000.25
        "C" locale with en_US numpunct: 1,000.25

🧚 Defect reports 🧚

okay so these behavior-changing defect reports were applied retroactively to previously published C++ standards, and this is the lore you need:

  • LWG 14 β€” applied to C++98: locale::combine was non-const; correct behavior: made const. periodt.
  • LWG 436 β€” applied to C++98: it was unclear whether Facet can be cv-qualified; correct behavior: it can be const-qualified, but not volatile-qualified. iconic clarification, honestly.

πŸ‘‘ See also πŸ‘‘

  • constructor β€” constructs a new locale (public member function).

mwah πŸ’‹ this little page comes from http://cppreference.com, version/date 2024.06.10 β€” stan the C++ Standard Libary docs, fr fr πŸ«ΆπŸ’œ

πŸ“¦ where this page comes from