How do I get a code point literal in utf8

Viewed 2099

I just recently realized, the u8 character prefix for C++17 is not meant for all utf8 code points, just for the ASCII part.

From cppreference

UTF-8 character literal, e.g. u8'a'. Such literal has type char and the value equal to ISO 10646 code point value of c-char, provided that the code point value is representable with a single UTF-8 code unit. If c-char is not in Basic Latin or C0 Controls Unicode block, the program is ill-formed.

auto hello = u8'嗨';     // ill-formed
auto world = u8"世";     // not a character
auto what = 0xE7958C;    // almost human-readable
auto wrong = u8"錯"[0];  // not even correct

How do I get a code point literal in utf8 succinctly?

EDIT: For the people wondering how a utf8 code point may be stored, a way I find reasonable is like the way Golang does it. The basic idea is to store a single code point in a 32-bit type when only a single code point is required.

EDIT2: From the arguments put out by the helpful comments, there is no reason to have encoded utf8 stay in a 32-bit type all along. Either have it decoded, which would be utf32 and have the prefix U, or have it encoded in a string, with the prefix u8.

2 Answers
Related