An object of class std::locale
is an immutable indexed set of immutable facets. Each stream object of the C++ input/output library is associated with an std::locale
object and uses its facets for parsing and formatting of all data. In addition, a locale object is associated with each std::basic_regex object.(since C++11) Locale objects can also be used as predicates that perform string collation with the standard containers and algorithms and can be accessed directly to obtain or modify the facets they hold.
Each locale constructed in a C++ program holds at least the following standard facets (i.e. std::has_facet returns true for all these facet types), but a program may define additional specializations or completely new facets and add them to any existing locale object.
Supported facets std::ctype<char>Internally, a locale object is implemented as if it is a reference-counted pointer to an array (indexed by std::locale::id) of reference-counted pointers to facets: copying a locale only copies one pointer and increments several reference counts. To maintain the standard C++ library thread safety guarantees (operations on different objects are always thread-safe), both the locale reference count and each facet reference count are updated in a thread-safe manner, similar to std::shared_ptr.
[edit] Member types Type Description the facet index type: each facet class must declare or inherit a public static member of this typeconst category none
[static]
a zero value indicating no facet categoryconst category collate
[static]
a bitmask value indicating the collate facet categoryconst category ctype
[static]
a bitmask value indicating the ctype facet categoryconst category monetary
[static]
a bitmask value indicating the monetary facet categoryconst category numeric
[static]
a bitmask value indicating the numeric facet categoryconst category time
[static]
a bitmask value indicating the time facet categoryconst category messages
[static]
a bitmask value indicating the messages facet categoryconst category all
[static]
collate | ctype | monetary | numeric | time | messagesstd::locale
member functions expecting a category
argument require one of the category values defined above, or the union of two or more such values. The LC
constants are not accepted.
Demonstrates the typical prologue of a locale-sensitive program (cross-platform).
#include <iostream> #include <locale> int main() { std::wcout << L"User-preferred locale setting is " << std::locale("").name().c_str() << L'\n'; // on startup, the global locale is the "C" locale std::wcout << 1000.01 << L'\n'; // replace the C++ global locale and the "C" locale with the user-preferred locale std::locale::global(std::locale("")); // use the new global locale for future wide character output std::wcout.imbue(std::locale()); // output the same number again std::wcout << 1000.01 << L'\n'; }
Possible output:
User-preferred locale setting is en_US.UTF8 1000.01 1,000.01[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 340 C++98 the set of standard facets that all locales need to hold was unclear made clear LWG 347 C++98 parameters of typecategory
could accept LC
constants not accepted anymore [edit] See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4