std::locale::locale

< cpp‎ | locale‎ | locale
在标头 <locale> 定义
(1)
locale() throw();
(C++11 前)
locale() noexcept;
(C++11 起)
(2)
locale( const locale& other ) throw();
(C++11 前)
locale( const locale& other ) noexcept;
(C++11 起)
explicit locale( const char* std_name );
(3)
explicit locale( const std::string& std_name );
(4) (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) (C++11 起)
template< class Facet >
locale( const locale& other, Facet* f );
(7)
locale( const locale& other, const locale& one, category cats );
(8)

构造新的本地环境对象。

1) 默认构造函数。构造全局 C++ 本地环境的副本,它是最近用作 std::locale::global 的实参的本地环境,或在未曾调用 std::locale::global 时是 std::locale::classic() 的副本。
2) 复制构造函数。构造 other 的副本。
3) 构造拥有指定 std_name 的系统本地环境(如 "C""POSIX""en_US.UTF-8""English_US.1251")的副本,如果操作系统支持这种本地环境。以此方式构造的本地环境拥有名称。
4) 等价于 locale(std_name.c_str())
5) 构造 other 的副本,但不包括 cats 实参所鉴别的所有刻面,这些刻面从它们的 std_name 所标识的系统本地环境复制。以此方式构造的本地环境当且仅当 other 拥有名称时拥有名称。
6) 等价于 locale(other, std_name.c_str(), cats)
7) 构造 other 的副本,不包括 Facet 类型的刻面(典型地从实参类型推导),该刻面从实参 f 安装。如果 f 是空指针,那么构造的本地环境是 other 的完整副本。如果 Facet 不是刻面或者是有 volatile 限定的刻面,那么程序非良构。
如果 f 为空,构造的本地环境的名称与 other 的名称相同。否则构造的本地环境没有名称。
8) 构造 other 的副本,不包括 cats 实参所鉴别的所有刻面,这些刻面从 one 复制。如果 otherone 都拥有名称,那么产生的本地环境也拥有名称。
如果 cats 等于 locale::none,那么构造的本地环境只有在 other 有名字时才会有名字。否则构造的本地环境只有在 otherone 都有名字时才会有名字。

参数

other - 要复制的另一本地环境
std_name - 要使用的系统本地环境名称
f - 指向要与 other 合并的刻面的指针
cats - 用于鉴别要与 other 合并的刻面的刻面类别
one - 接收刻面来源的另一本地环境

异常

3,5) 如果操作系统没有名为 std_name 的本地环境,或 std_name 是空指针,那么就会抛出 std::runtime_error
4,6) 如果操作系统没有名为 std_name 的本地环境,那么就会抛出 std::runtime_error

注解

典型地以 f 为第二实参调用重载 (7),该实参从 new 表达式直接获取:本地环境负责从它自身的析构函数调用匹配的 delete

示例

#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 为经典 "C" 本地环境副本
    std::locale l1; 
    // l2 为 Unicode 本地环境
    std::locale l2("en_US.UTF-8");
    // l3 为 "C",除了 ctype 为 Unicode
    std::locale l3(l1, l2, std::locale::ctype);
    // l4 为 "C",除了 codecvt
    std::locale l4(l1, new std::codecvt_utf8<wchar_t>);
 
    std::cout
        << "本地环境名称:\n"
        << "l1: " << l1 << "l2: " << l2
        << "l3: " << l3 << "l4: " << l4;
}

可能的输出:

本地环境名称:
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: *

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 436 C++98 对于重载 (7),不明确 Facet 是否可以有 cv 限定 可以有 const 限定,但不能有 volatile 限定
LWG 2295 C++98 对于重载 (7),即使 f 为空,构造的本地环境也没有名称 此时它的名称是 other 的名称

参阅

析构本地环境和其引用计数变为零的刻面
(公开成员函数)

外部链接

1.  Windows 本地环境名称列表
2.  Linux 本地环境名称列表