std::text_encoding::literal

From cppreference.com
 
 
 
 
static consteval text_encoding literal() noexcept;
(since C++26)

Constructs a new text_encoding object representing the ordinary character literal encoding. It is used to determine the character encoding applied to ordinary character or string literals (e.g. "This is literal").

This function is deleted unless the CHAR_BIT is 8.

Parameters

(none)

Return value

The object holding the representation of the ordinary literal encoding.

Notes

This function can be implemented by constructing text_encoding with compiler-specific builtin macros such as __clang_literal_encoding__ from Clang, or __GNUC_EXECUTION_CHARSET_NAME from GCC. These macros, which are known at compile-time, expand to a narrow string literal containing the name of the narrow execution character set used (ordinary literal encoding).

The value returned by literal() may depend on compiler options such as -fexec-charset=encoding-name in GCC or Clang or /execution-charset:encoding-name in MSVC.

Example

This example demonstrates the assertion that the ordinary literal encoding should be 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}";
}