Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

char8_t

Use case

Distint type for UTF-8 strings.

Explanation

char8_t is a distinct type for UTF-8. Prevents accidentally mixing UTF-8 with other encodings.

Code

#include <cstdint>
#include <iostream>

int main() {
  // https://www.utf8-chartable.de/
  const char8_t *utf8_str = u8"_main\u0023";
  std::cout << "Size: " << sizeof(char8_t) << " byte.\n";
  std::cout << reinterpret_cast<const char *>(utf8_str) << "\n";
  return 0;
}

View on GitHub.

Output

$ ./src/c++20/build/char8_t
Size: 1 byte.
_main#