名稱空間
變體
操作

std::text_encoding::literal

來自 cppreference.com
< cpp‎ | text‎ | text_encoding
static consteval text_encoding literal() noexcept;
(C++26 起)

構造一個表示普通字元字面量編碼的新的text_encoding物件。它用於確定應用於普通字元或字串字面量(例如"This is literal")的字元編碼。

除非CHAR_BIT8,否則此函式被刪除。

目錄

[編輯] 引數

(無)

[編輯] 返回值

持有普通字面量編碼表示的物件。

[編輯] 注意

此函式可以透過使用編譯器特定的內建宏來構造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}";
}