Update 2022 Jul 28
P2513R4, char8_t Compatibility and Portability Fix, Draft Proposal, 2022-06-17
Two years after and char8_t
definition (or lack of) is now called a "C++20 defect" and there is a rush to fix it. Finally.
Update 2020 Aug 25
The question seems somewhat irrelevant in the light of this:
// GCC 10.2, clang 10.0.1 -std=c++20
int main(int argc, char ** argv)
{
char32_t single_glyph_32 = U'ア' ;
char16_t single_glyph_16 = u'ア' ;
// gcc: error: character constant too long for its type
// clang: error: character too large for enclosing character literal type
char8_t single_glyph_8 = u8'ア' ;
return 42;
}
char8_t seems capable of handling just a tiny portion of UTF-8 glyphs. Thus there is no much point in using it or trying to printf it.
Asked Nov 15 '19 at 14:04
And also for char8_t
?
I assume there is some C++20 decision, somewhere, but I could not find it.
There is also P1428, but that doc is not mentioning anything about printf()
family v.s. char8_t *
or char8_t
.
Use std::cout
advice might be an answer. Unfortunately, that does not compile anymore.
// does not compile under C++20
// error : overload resolution selected deleted operator '<<'
// see P1423, proposal 7
std::cout << u8"A2";
std::cout << char8_t ('A');
For C 2.x and char8_t
Please start from here.
Update
I have done some more tests with a single element from a u8 sequence.
And that indeed does not work. char8_t *
to printf("%s")
does work, but char8_t
to printf("%c")
is an accident waiting to happen.
Please see -- https://wandbox.org/permlink/6NQtkKeZ9JUFw4Sd -- Problem is, as per the current status quo, char8_t
is not implemented, char8_t *
is. -- let me repeat: there is no implemented type to hold a single element from a char8_t *
sequence.
If you want a single u8 glyph you need to code it as an u8 string
char8_t const * single_glyph = u8"ア";
And it seems at present, to print the above the sort of a sure way is
// works with warnings
std::printf("%s", single_glyph ) ;
To start reading on this subject, probably these two papers are required
- http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2231.htm
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1423r2.html
In that order.
My primary DEVENV is VisualStudio 2019, with both MSVC and CLANG 8.0.1, as delivered with VS. With std:c++latest. Dev machine is WIN10 [Version 10.0.18362.476]