Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::u8string — a string of UTF-8 code units

std::u8string is an alias for std::basic_string<char8_t> from <string>. It stores UTF-8 code units but does not validate the encoding; size() does not count user-perceived characters.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <string>
std::u8string text = u8"...";
// Alias: std::basic_string<char8_t>

std::u8string from <string> is an alias for std::basic_string<char8_t>. It owns contiguous storage and supports changing the string's length. Initialize it with a UTF-8 literal, for example std::u8string text = u8"A\u00E9";. The u8 prefix produces a literal with elements of type char8_t.

size() and indexing concern code units, not Unicode code points or user-perceived characters. In the example, A, é, and ! occupy four UTF-8 units altogether. An operation at a single index can split a character's multi-unit encoding. The container neither validates UTF-8 nor performs Unicode normalization.

c_str() exposes a const char8_t*; the trailing zero is not included in size(). This is not const char*: std::u8string does not implicitly convert to std::string and cannot be printed directly with std::cout << text. Integration with a char-based interface requires explicit handling of the representation and encoding contract.

This entry targets C++23. The repository's specified compilation standard does not establish production runtime compatibility. The eel.is source is an evolving draft, not a frozen C++23 publication.

Examples

#include <iostream>
#include <string>

int main() {
    std::u8string text = u8"A\u00E9";
    text += u8"!";
    std::cout << "code units: " << text.size() << '\n';
    std::cout << "terminator: "
              << static_cast<unsigned int>(text.c_str()[text.size()])
              << '\n';
}

Result

code units: 4
terminator: 0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources