std::locale::locale(3)
β¨ std::locale::locale(3) β¨ from the C++ Standard Libary manual β okay bestie, this page is all about constructing std::locale objects, and the overload set is lowkey giving βpick your fighterβ energy. Letβs go through every single one, no cap. π«Ά
π NAME π
std::locale::locale - std::locale::locale
β¨ SYNOPSIS β¨
Defined in header <locale> β yes bestie, thatβs where all the locale magic lives.
locale() throw(); (1) (until C++11)
locale() noexcept; (1) (since C++11)
locale( const locale& other ) throw(); (2) (until C++11)
locale( const locale& other ) noexcept; (2) (since C++11)
explicit locale( const char* std_name ); (3)
explicit locale( const std::string& std_name ); (4) (since C++11)
locale( const locale& other, const char* std_name, category cats ); (5)
locale( const locale& other, const std::string& std_name, category cats ); (6) (since C++11)
template< class Facet > (7)
locale( const locale& other, Facet* f );
locale( const locale& other, const locale& one, category cats ); (8)
π DESCRIPTION π
Constructs a new locale object. Periodt. π
- 1) Default constructor. Constructs a copy of the global C++ locale, which is the locale most recently used as the argument to
std::locale::globalor a copy ofstd::locale::classic()if no call tostd::locale::globalhas been made. - 2) Copy constructor. Constructs a copy of
other. - 3,4) Constructs a copy of the system locale with specified
std_name(such as"C", or"POSIX", or"en_US.UTF-8", or"English_US.1251"), if such locale is supported by the operating system. The locale constructed in this manner has a name. - 4) Equivalent to
locale(std_name.c_str()). - 5) Constructs a copy of
otherexcept for all the facets identified by thecatsargument, which are copied from the system locale identified by itsstd_name. The locale constructed in this manner has a name if and only ifotherhas a name. - 6) Equivalent to
locale(other, std_name.c_str(), cats). - 7) Constructs a copy of
otherexcept for the facet of typeFacet(typically deduced from the type of the argument) which is installed fromf. Iffis a null pointer, the constructed locale is a full copy ofother. The program is ill-formed ifFacetis not a facet or it is a volatile-qualified facet. Iffis null, the constructed locale has the same name asother. Otherwise, the constructed locale has no name. - 8) Constructs a copy of
otherexcept for all the facets identified by thecatsargument, which are copied fromone. Ifcatsis equal tolocale::none, the constructed locale has a name if and only ifotherhas a name. Otherwise, the constructed locale has a name if and only ifotherandoneboth have names.
π PARAMETERS π
otherβ another locale to copy.std_nameβ name of the system locale to use.fβ pointer to a facet to merge withother.catsβ the facet categories used to identify the facets to merge withother.oneβ another locale to take facets from.
π¨ EXCEPTIONS π¨
3,5)βstd::runtime_errorif the operating system has no locale namedstd_nameor ifstd_nameis a null pointer.4,6)βstd::runtime_errorif the operating system has no locale namedstd_name.
π NOTES π
Overload (7) is typically called with its second argument, f, obtained directly from a new-expression: the locale is responsible for calling the matching delete from its own destructor.
π§ͺ EXAMPLE π§ͺ
// Run this code
#include <codecvt>
#include <iostream>
#include <locale>
std::ostream& operator<< (std::ostream& os, std::locale const& loc)
{
if (loc.name().length() <= 80)
return os << loc.name() << '\n';
for (const auto c : loc.name())
c != ';' ? os << c : os << "\n ";
return os << '\n';
}
int main()
{
// l1 is a copy of the classic "C" locale
std::locale l1;
// l2 is a unicode locale
std::locale l2("en_US.UTF-8");
// l3 is "C" except for ctype, which is unicode
std::locale l3(l1, l2, std::locale::ctype);
// l4 is "C" except for codecvt
std::locale l4(l1, new std::codecvt_utf8<wchar_t>);
std::cout
<< "Locale names:\n"
<< "l1: " << l1 << "l2: " << l2
<< "l3: " << l3 << "l4: " << l4;
}
π Possible output π
Locale names:
l1: C
l2: en_US.UTF-8
l3: LC_CTYPE=en_US.UTF-8
LC_NUMERIC=C
LC_TIME=C
LC_COLLATE=C
LC_MONETARY=C
LC_MESSAGES=C
LC_PAPER=C
LC_NAME=C
LC_ADDRESS=C
LC_TELEPHONE=C
LC_MEASUREMENT=C
LC_IDENTIFICATION=C
l4: *
π DEFECT REPORTS π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR
LWG 436β Applied to:C++98. Behavior as published: for overload (7), it was unclear whetherFacetcan be cv-qualified. Correct behavior: it can be const-qualified, but not volatile-qualified. - DR
LWG 2295β Applied to:C++98. Behavior as published: for overload (7), the constructed locale had no name even iffis null. Correct behavior: it has the name ofother.
π SEE ALSO π
- destructor β destructs the locale and the facets whose reference count becomes zero (
public member function).
π EXTERNAL LINKS π
- List of Windows locale names.
- List of Linux locale names.
source/version/date: http://cppreference.com, 2024.06.10, std::locale::locale(3) β bye bestie, go construct something iconic π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms