Namespaces
Variants

std:: locale

From cppreference.net
定义于头文件 <locale>
class locale ;

std::locale 的对象是一个不可变的、按索引组织的不可变刻面集合。C++ 输入/输出库中的每个流对象都与一个 std::locale 对象相关联,并使用其刻面对所有数据进行解析和格式化。 此外,每个 std::basic_regex 对象也会关联一个区域设置对象。 (C++11 起) 区域设置对象还可作为谓词用于标准容器和算法中的字符串排序,并可直接访问以获取或修改其所持有的刻面。

每个在C++程序中构造的locale至少包含以下标准facet(即 std::has_facet 对所有facet类型均返回 true ),但程序可以定义额外的特化或全新的facet,并将其添加到任何现有的locale对象中。

支持的 facets
std:: ctype < char >
std:: ctype < wchar_t >
std:: codecvt < char , char , std:: mbstate_t >
std:: codecvt < wchar_t , char , std:: mbstate_t >
std:: num_get < char >
std:: num_get < wchar_t >
std:: numpunct < char >
std:: numpunct < wchar_t >
std:: num_put < char >
std:: num_put < wchar_t >
std:: money_get < char >
std:: money_get < wchar_t >
std:: moneypunct < char >
std:: moneypunct < char , true >
std:: moneypunct < wchar_t >
std:: moneypunct < wchar_t , true >
std:: money_put < char >
std:: money_put < wchar_t >
std:: time_get < char >
std:: time_get < wchar_t >
std:: collate < char >
std:: collate < wchar_t >
std:: time_put < char >
std:: time_put < wchar_t >
std:: messages < char >
std:: messages < wchar_t >
已弃用的 facets
std:: codecvt < char16_t , char , std:: mbstate_t > (自 C++11 起) (在 C++20 中弃用)
std:: codecvt < char32_t , char , std:: mbstate_t > (自 C++11 起) (在 C++20 中弃用)
std:: codecvt < char16_t , char8_t, std:: mbstate_t > (自 C++20 起) (已弃用)
std:: codecvt < char32_t , char8_t, std:: mbstate_t > (自 C++20 起) (已弃用)

在内部,locale对象被实现为一个引用计数的指针,指向由 std::locale::id 索引的facet引用计数指针数组:复制locale仅复制一个指针并增加若干引用计数。为维护标准C++库的线程安全保证(对不同对象的操作始终是线程安全的),locale引用计数和每个facet引用计数都以线程安全的方式更新,类似于 std::shared_ptr 的实现机制。

目录

成员类型

类型 描述
刻面索引类型:每个刻面类必须声明或继承此类型的公共静态成员
(类)
所有刻面类别的基类:任何类别的每个刻面都派生自此类型
(类)
category
int
(类型定义)

成员常量

名称 说明
const category none
[static]
表示无facet类别的零值
(公开静态成员常量)
const category collate
[static]
表示collate facet类别的位掩码值
(公开静态成员常量)
const category ctype
[static]
表示ctype facet类别的位掩码值
(公开静态成员常量)
const category monetary
[static]
表示monetary facet类别的位掩码值
(公开静态成员常量)
const category numeric
[static]
表示numeric facet类别的位掩码值
(公开静态成员常量)
const category time
[static]
表示time facet类别的位掩码值
(公开静态成员常量)
const category messages
[static]
表示messages facet类别的位掩码值
(公开静态成员常量)
const category all
[static]
collate | ctype | monetary | numeric | time | messages
(公开静态成员常量)

std::locale 需要 category 参数的成员函数要求使用上述定义的类别值,或两个及以上此类值的并集。不接受 LC 常量

成员函数

构造新的 locale 对象
(公开成员函数)
析构 locale 对象及引用计数归零的 facet
(公开成员函数)
替换 locale 对象
(公开成员函数)
构造包含从另一 locale 复制的编译时识别 facet 的新 locale
(公开成员函数)
返回 locale 名称,若未命名则返回 "*"
(公开成员函数)
(C++26)
返回与 locale 关联的字符编码方案
(公开成员函数)
(removed in C++20)
locale 对象间的相等性比较
(公开成员函数)
使用此 locale 的 collate facet 对两个字符串进行字典序比较
(公开成员函数)
[static]
更改全局 locale
(公开静态成员函数)
[static]
获取对 "C" locale 的引用
(公开静态成员函数)

示例

演示区域设置敏感程序的典型序言(跨平台)。

#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';
}

可能的输出:

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

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 340 C++98 所有区域设置需要包含的标准facet集合不明确 已明确说明
LWG 347 C++98 category 类型的参数可以接受 LC 常量 不再接受

另请参阅

描述用于访问 IANA 字符集注册表 的接口
(类)
从本地化环境中获取一个 facet
(函数模板)
检查本地化环境是否实现特定 facet
(函数模板)
设置本地化环境
( std::ios_base 的公开成员函数)
返回当前本地化环境
( std::ios_base 的公开成员函数)