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/