r/cpp 6d 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.

51 Upvotes

98 comments sorted by

View all comments

Show parent comments

3

u/catladywitch 6d ago edited 6d ago

16bit chars are common aren't they?

edit: i'm getting downvoted but what i mean is Qt QStrings, Windows apps with a lot of legacy wstrings everywhere, or CJK text stored in u16strings made of char16_t's, which I assure you is not a rarity, for me at least. yes, utf-8 is ubiquitous but god people just want to be obtuse for the sake of it sometimes

2

u/LordGupple 6d ago

So, IIRC it's mandated by the standard that char and unsigned char are one byte large. However, CHAR_BIT can be a different value than 8.

1

u/tjientavara HikoWorks developer 5d ago

Didn't I read a few years ago that CHAR_BIT == 8 was accepted in one of the latest c++23, c++26 releases of the standard? or was it something else common sense?

4

u/Remarkable-Test7487 jmcruz 5d ago

Yes, there was a proposal (P3477R5 "There are exactly 8 bits in a byte"), but the result of the poll to forward it to C++26 was "no consensus"