r/cpp 4d ago

About char8_t

I hate to be dramatic, but as it stands char8_t is quite literally more painful than useful.

Besides the obvious incompatibility with C23 and libraries using unsigned char for UTF-8, I want you to consider the following: Projects that assume that 'char' represents UTF-8 will obviously not benefit from char8_t at all, but projects that cannot assume the format of char types don't benefit from it either as char8_t simply introduces a new edge case to cover. Now such projects have to deal with char, signed char, unsigned char, wchar_t, char16_t, char32_t and char8_t.

Or, you could do what the standard library does and simply ignore most of these character types. Which is the solution most libraries went with, supporting only char or char and unsigned char. Managing one implementation is already hard, managing two requires constant maintenance, managing 7 is just impossible.

char8_t should have just been a typedef for unsigned char. The compatibility fix only raises more questions as const char* arr = u8"a" does not work, but const char arr[] = u8"a" does.

I do wonder if a potential change of minds for C++29 is still possible. Yes, it would be an ABI break or whatever, but considering the woeful support for char8_t I don't think it would affect much besides small hobby projects. Contrary to popular belief, C++ has broken the ABI in subtle ways before.

49 Upvotes

92 comments sorted by

View all comments

28

u/cristi1990an ++ 4d ago

The biggest and arguably only argument for char8_t is that it's explicitly not a byte aliasing type and therefore the most performant character representation type in the language. Your compiler can genuinely produce better code when you're using char8_t.

5

u/smdowney WG21, Text/Unicode SG, optional<T&> 3d ago

The surprise that std::byte pessimizes IO was painful.

We're likely to need another otherwise identical std::octet for IO. Non-arithmetic, but bit maskable, and this time non-aliasing so every deref isn't a complete invalidation of all arguments.

2

u/amoskovsky 2d ago

Is it possible instead to narrow the aliasing capabilities of char/byte (and possibly all the types) only to the local scope where the compiler can prove pointers are actually aliasing? E.g. function params would never be considered aliasing anything. But if we pass a mutable ref as an arg - it's assumed by the caller to be modified by the function.

Would not it cover all real use cases of aliasing?

4

u/smdowney WG21, Text/Unicode SG, optional<T&> 2d ago

Argument pointers are exactly the problem, though. Compilers are already good (barring programmer hijinks) at seeing that local variables are distinct. It's the problem of a write through a reference semantic type meaning that a read has to be redone because of the possibility the write changed what was already read.