Namespaces
Variants

std::text_encoding:: literal

From cppreference.net
static consteval text_encoding literal ( ) noexcept ;
(C++26以降)

text_encoding オブジェクトを新しく構築し、 通常文字リテラルエンコーディング を表現します。これは通常の文字または文字列リテラル(例: "This is literal" )に適用される文字エンコーディングを決定するために使用されます。

この関数は、 CHAR_BIT 8 でない限り削除されます。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、リンク先は一切変更していません - ` `内のテキストはC++関連の専門用語(Parameters, Return value, Notes, Example)であるため、翻訳せずにそのまま保持しています - 番号部分と書式は完全に維持しています

パラメータ

(なし)

戻り値

通常リテラルエンコーディングの表現を保持するオブジェクト。

注記

この関数は、Clangの __clang_literal_encoding__ やGCCの __GNUC_EXECUTION_CHARSET_NAME といったコンパイラ固有の組み込みマクロを使用して text_encoding を構築することで実装できます。これらのコンパイル時に既知のマクロは、使用されているナロー実行文字セット(通常のリテラルエンコーディング)の名前を含むナロー文字列リテラルに展開されます。

literal ( ) によって返される値は、 -fexec-charset= encoding-name のようなGCCやClangのコンパイラオプション、または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}";
}