c++ - 如何将 unicode 点 U+10000 编码为 char16_t 字符串文字?

标签 c++ unicode c++11

C++11 标准中的第 2.14.5p9 段说(强调我的):

A string literal that begins with u, such as u"asdf", is a char16_t string literal. A char16_t string literal has type “array of n const char16_t”, where n is the size of the string as defined below; it has static storage duration and is initialized with the given characters. A single c-char may produce more than one char16_t character in the form of surrogate pairs.

如何在 char16_t 字符串文字中编码由代理项对 D800(高代理项)和 DC00(低代理项)组成的 unicode 点 U+10000?

上面引用的文本中提到的c-char是什么意思?

最佳答案

实际上非常简单: u"\U00010000" 。正如文中所述,单个 c-char (在本例中为 \U00010000 )可以生成多个 char16_t 。快速测试将显示 u"\U00010000" 的类型为 char16_t const[3](代理对为 2 + 空终止符为 1)。

或者,您可以简单地将字符直接放入字符串中,如 u"𐀀" ,假设源文件使用编译器期望的编码保存。这将是完全相同的事情,因为对源文件执行的第一步是将基本字符集之外的任何字符替换为其通用字符名(即 \U00010000 )。 p>

c-char 是“源字符集的任何成员,除了单引号 ' 、反斜杠 \ 或换行符”,或者 转义序列 em> (如 \n\123\x41 ),或通用字符名称 (如 \u1234\U12345678 )。

标准文档在末尾处有一个语法产生式索引,您可以使用它来查找这些内容。

关于c++ - 如何将 unicode 点 U+10000 编码为 char16_t 字符串文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17474090/

相关文章:

带有解析/匹配示例的 C++ BNF 语法

c - libunistring u8_strlen() 等于 strlen()?

delphi - CP_UTF8 是 WriteConsoleA/WriteFile 支持的代码页吗?

c++ - 为什么 mem_fn() 提示尝试使用已删除的函数?

c++ - 将 boost::chrono 计时机制包装到类编译错误中?

c++ - 需要对条件变量寻求的互斥保护(原子)赋值吗?

javascript - .toUpperCase 将一些字符一分为二? .toLowerCase 也可以这样做吗?

c++ - 在这种情况下,while 循环是如何工作的?

c++ - 通过 std::transform 将 std::map 展平为一个集合

c++ - 段错误重载运算符 <<