std::text_encoding::literal
来自 cppreference.cn
< cpp | text | text_encoding
static consteval text_encoding literal() noexcept; |
(C++26 起) | |
构造一个表示普通字符字面量编码的新的text_encoding
对象。它用于确定应用于普通字符或字符串字面量(例如"This is literal")的字符编码。
除非CHAR_BIT为8,否则此函数被删除。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
持有普通字面量编码表示的对象。
[编辑] 注意
此函数可以通过使用编译器特定的内置宏来构造text_encoding
,例如Clang的__clang_literal_encoding__
或GCC的__GNUC_EXECUTION_CHARSET_NAME
。这些在编译时已知的宏,会扩展为一个窄字符串字面量,其中包含所使用的窄执行字符集(普通字面量编码)的名称。
literal()返回的值可能取决于编译器选项,例如GCC或Clang中的-fexec-charset=encoding-name
,或MSVC中的/execution-charset:encoding-name
。
[编辑] 示例
此示例演示了普通字面量编码应为UTF-8的断言。
运行此代码
#include <text_encoding> static_assert(std::text_encoding::literal() == std::text_encoding::UTF8); int main() { // if the literal encoding is UTF-8, then this unprefixed string literal is // encoded as UTF-8 constexpr char green_heart[] = "\N{GREEN HEART}"; // this prefixed string literal is always encoded as UTF-8 regardless of the // literal encoding constexpr char8_t green_heart_u8[] = u8"\N{GREEN HEART}"; }