c++ - std::ctype 是否总是按 "C"语言环境对字符进行分类?

标签 c++ unicode character-encoding locale

cppreference 表示 std::ctype 提供基于经典“C”语言环境的字符分类。当我们创建这样的语言环境时,这甚至是真的吗:

std::locale loc(std::locale("en_US.UTF8"), new std::ctype<char>);

loc 的方面是否仍会根据“C”语言环境或 Unicode 语言环境对字符进行分类?如果按前者分类,为什么我们还要将语言环境名称指定为“en_US.UTF8”?

最佳答案

标准要求默认构造 std::ctype<char>通过 §22.4.1.3.3[facet.ctype.char.statics]/1 匹配最小的“C”语言环境

static const mask* classic_table() noexcept;

Returns: A pointer to the initial element of an array of size table_size which represents the classifications of characters in the "C" locale

分类成员函数is()根据 table() 定义这是根据 classic_table() 定义的除非向 ctype<char> 提供了另一个表的构造函数

我更新了 cppreference 以更恰本地匹配这些要求(它也为 std::ctype<wchar_t> 说“C”)

要回答您的第二个问题,用 std::locale loc(std::locale("en_US.UTF8"), new std::ctype<char>); 构建的语言环境将使用您指定的 ctype 方面(因此,“C”)对窄字符进行分类,但这是多余的:narrow character classification of a plain std::locale("en_US.UTF8") (至少在 GNU 实现中)是完全一样的:

#include <iostream>
#include <cassert>
#include <locale>
int main()
{

    std::locale loc1("en_US.UTF8");
    const std::ctype_base::mask* tbl1 =
         std::use_facet<std::ctype<char>>(loc1).table();

    std::locale loc2(std::locale("en_US.UTF8"), new std::ctype<char>);
    const std::ctype_base::mask* tbl2 =
         std::use_facet<std::ctype<char>>(loc2).table();

    for(size_t n = 0; n < 256; ++n)
        assert(tbl1[n] == tbl2[n]);
}

关于c++ - std::ctype 是否总是按 "C"语言环境对字符进行分类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681882/

相关文章:

c++ - 错误 c2259 'class' : cannot instantiate abstract class

c++ - 我正在为移动设备构建触摸屏应用程序。我应该使用什么 C++ 工具包?

c++ - Windows 服务通信选项

unicode - 组合变音符号的特性

java - 无法在我的 servlet 中解码西里尔字符串

c++ - 如何根据数组元素自动创建模板类的元组?

Jquery插入unicode而不是符号

MySQL 输出十六进制字符串为 UTF-8

java - InputStreamReader 缓冲问题

unicode - UTF-8编码为什么前缀10?