c++11 标准模板(STL)(std::locale)(二)构造函数

用以封装文化差异的多态刻面的集合

std::locale 类型对象是不可变平面的不可变索引集。 C++ 输入/输出库的每个流对象与一个 std::locale 对象关联,并用其平面分析及格式化所有数据。另外, locale 对象与每个 std::basic_regex 对象关联。 locale 对象亦可在标准容器和算法中用作进行字符串对照的谓词,而且能被直接访问,以获得或修改其所保有的平面。

C++ 程序中构造的每个 locale 至少保有下列标准平面,但程序可以定义额外特化,或全新的平面,并将它们添加到任何既存的 locale 对象。

构造新的 locale

std::locale::locale

locale() throw();

(1)(C++11 前)

locale() noexcept;

(C++11 起)

locale( const locale& other ) throw();

(2)(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 cat );

(5)

locale( const locale& other, const std::string& std_name, category cat );

(6)(C++11 起)

template< class Facet >
locale( const locale& other, Facet* f );

(7)

locale( const locale& other, const locale& one, category cat );

(8)

 构造新的 locale 对象。

1) 默认构造函数。构造全局 C++ 本地环境的副本,它是最近用作 std::locale::global 的参数的 locale ,或若未曾调用 std::locale::global 则为 std::locale::classic 的副本。

2) 复制构造函数。构造 other 的副本。

3-4) 构造拥有特定 std_name 的系统本地环境(如 "C" 或 "POSIX" 或 "en_US.UTF-8" 或 "English_US.1251" )的副本,若操作系统支持这种本地环境。以此方式构造的 locale 拥有名称。

5-6) 构造 other 的副本,除了 cat 参数所鉴别的所有平面,这些平面从其 std_name 所标识的系统本地环境复制。以此方式构造的 locale 拥有名称,当且仅当 other 拥有名称。

7) 构造 other 的副本,除了 Facet 类型平面(典型地从实参类型推导),该平面从参数 facet 安装。若 facet 为 NULL ,则构造的 locale 为 other 的完整副本。以此方式构造的 locale 无名称。

8) 构造 other 的副本,除了 cat 参数所鉴别的所有平面,这些平面从 one 复制。若 otherone 都拥有名称,则产生的 locale 拥有名称。

参数

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

异常

3,5) 若操作系统无名为 std_name 的本地环境,或若 std_name 为 NULL 则为 std::runtime_error 。

4,6) 若操作系统无名为 std_name 的本地环境则为 std::runtime_error 。

7,8) (无)

注意

典型地以直接获得自 new 表达式的参数 f 为第二参数调用重载 7 : locale 负责从其自身的析构函数调用匹配的 delete。

调用示例

#include <iostream>
#include <locale>
#include <codecvt>

struct myfacet : std::locale::facet
{
    myfacet(std::size_t refs = 0) : facet(refs) {}
    static std::locale::id id;
};

//类 std::locale::id 提供本地环境片面的实现限定标识。
std::locale::id myfacet::id;

int main()
{
    //1) 默认构造函数。构造全局 C++ 本地环境的副本,它是最近用作 std::locale::global 的参数的 locale ,
    //或若未曾调用 std::locale::global 则为 std::locale::classic 的副本。
    std::locale locale1;  // locale1 是经典 "C" 本地环境的副本
    std::cout << "locale1.name():   " << locale1.name() << std::endl;

    std::locale locale2("Chinese (Simplified)_China.936"); // locale2 为本地环境
    std::cout << "locale2.name():   " << locale2.name() << std::endl;

    std::locale locale3(locale1, locale2, std::locale::ctype); // locale3 为 "C" ,除了 ctype 为 unicode
    std::cout << "locale3.name():   " << locale3.name() << std::endl;

    std::locale locale4(locale1, new std::codecvt_utf8<wchar_t>); // locale4 为 "C" ,除了 codecvt
    std::cout << "locale4.name():   " << locale4.name() << std::endl;

    //2) 复制构造函数。构造 other 的副本。
    std::locale locale5(locale2); // locale2 为本地环境
    std::cout << "locale5.name():   " << locale5.name() << std::endl;

    //3-4) 构造拥有特定 std_name 的系统本地环境(如 "C" 或 "POSIX" 或 "en_US.UTF-8" 或 "English_US.1251" )的副本,
    //若操作系统支持这种本地环境。以此方式构造的 locale 拥有名称。
    std::locale locale6("English_US.1251");
    std::cout << "locale6.name():   " << locale6.name() << std::endl;

    std::string strlocale7 = "English_US.1251";
    std::locale locale7(strlocale7);
    std::cout << "locale7.name():   " << locale7.name() << std::endl;

    //5-6) 构造 other 的副本,除了 cat 参数所鉴别的所有平面,这些平面从其 std_name 所标识的系统本地环境复制。
    //以此方式构造的 locale 拥有名称,当且仅当 other 拥有名称。
    std::locale::category cat = 3;
    std::locale locale8(locale1, "English_US.1251", cat);
    std::cout << "locale8.name():   " << locale8.name() << std::endl;

    //7) 构造 other 的副本,除了 Facet 类型平面(典型地从实参类型推导),该平面从参数 facet 安装。
    //若 facet 为 NULL ,则构造的 locale 为 other 的完整副本。以此方式构造的 locale 无名称。
    std::locale locale9(locale1, new myfacet);
    std::cout << "locale9.name():   " << locale9.name() << std::endl;

    //8) 构造 other 的副本,除了 cat 参数所鉴别的所有平面,这些平面从 one 复制。
    //若 other 与 one 都拥有名称,则产生的 locale 拥有名称。
    std::locale locale10(locale1, locale2, cat);
    std::cout << "locale10.name():  " << locale10.name() << std::endl;
    return 0;
}

输出

locale1.name():   C
locale2.name():   Chinese (Simplified)_China.936
locale3.name():   Chinese (Simplified)_China.936
locale4.name():   *
locale5.name():   Chinese (Simplified)_China.936
locale6.name():   English_United States.1251
locale7.name():   English_United States.1251
locale8.name():   English_United States.1251
locale9.name():   C
locale10.name():  Chinese (Simplified)_China.936

析构 locale 和其引用计数变为零的平面

std::locale::~locale

~locale();

非虚析构函数,减少 *this 所保有的所有平面的引用计数。删除引用计数变为零的平面。

返回值

(无)

异常

(无)

  • 16
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值