Namespaces
Variants
Views
Actions

std::locale

From cppreference.com
< cpp‎ | locale
Revision as of 12:31, 23 March 2013 by SuperBoi45 (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td class="t-dcl-nopad">
class locale;
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

An object of class std::locale is an indexed set of reference-counted pointers to facets. Each stream object of the C++ input/output library is associated with an std::locale and uses its facets for parsing and formatting of all data. In addition, a locale object is associated with each basic_regex object. 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, but a program may define additional specializations or completely new facets and add them to any existing locale object.

Supported facets
std::collate<char> std::collate<wchar_t>
std::ctype<char> std::ctype<wchar_t>
std::codecvt<char,char,mbstate_t>
std::codecvt<char16_t,char,mbstate_t>
std::codecvt<char32_t,char,mbstate_t>
std::codecvt<wchar_t,char,mbstate_t>
std::moneypunct<char>
std::moneypunct<char,true>
std::moneypunct<wchar_t>
std::moneypunct<wchar_t,true>
std::money_get<char> std::money_get<wchar_t>
std::money_put<char> std::money_put<wchar_t>
std::numpunct<char> std::numpunct<wchar_t>
std::num_get<char> std::num_get<wchar_t>
std::num_put<char> std::num_put<wchar_t>
std::time_get<char> std::time_get<wchar_t>
std::time_put<char> std::time_put<wchar_t>
std::messages<char> std::messages<wchar_t>

Contents

Member types

Template:cpp/locale/locale/dcl list idTemplate:cpp/locale/locale/dcl list facet
category
int
(typedef)

Member objects

none
[static]
a zero value of type category indicating no facet category
(public static member constant)
collate
[static]
a bitmask value of type category indicating the collate facet category
(public static member constant)
ctype
[static]
a bitmask value of type category indicating the ctype facet category
(public static member constant)
monetary
[static]
a bitmask value of type category indicating the monetary facet category
(public static member constant)
numeric
[static]
a bitmask value of type category indicating the numeric facet category
(public static member constant)
time
[static]
a bitmask value of type category indicating the time facet category
(public static member constant)
messages
[static]
a bitmask value of type category indicating the messages facet category
(public static member constant)
all
[static]
collate | ctype | monetary | numeric | time | messages
(public static member constant)

Member functions

Template:cpp/locale/locale/dcl list localeTemplate:cpp/locale/locale/dcl list ~localeTemplate:cpp/locale/locale/dcl list operator=Template:cpp/locale/locale/dcl list combineTemplate:cpp/locale/locale/dcl list nameTemplate:cpp/locale/locale/dcl list operator cmpTemplate:cpp/locale/locale/dcl list operator()Template:cpp/locale/locale/dcl list globalTemplate:cpp/locale/locale/dcl list classic

Example

Demonstrates the typical prologue of a locale-sensitive program (cross-platform)

#include <iostream>
#include <locale>
 
int main()
{
    std::wcout << "User-preferred locale setting is " << std::locale("").name().c_str() << '\n';
    // on startup, the global locale is the "C" locale
    std::wcout << 1000.01 << '\n';
    // replace the C++ global locale as well as 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 << '\n';
}

Output:

User-preferred locale setting is en_US.UTF8
1000.01
1,000.01

See also

Template:cpp/locale/dcl list use facetTemplate:cpp/locale/dcl list has facetTemplate:cpp/io/ios base/dcl list imbueTemplate:cpp/io/ios base/dcl list getloc