【发布时间】:2020-03-11 16:52:08
【问题描述】:
2020 年 8 月 25 日更新
根据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 似乎只能处理一小部分 UTF-8 字形。因此,使用它或尝试打印它没有多大意义。
2019 年 11 月 15 日 14:04 提问
还有char8_t?
我假设某个地方有一些 C++20 决定,但我找不到它。
还有P1428,但该文档没有提及printf()family vs. char8_t * 或 char8_t。
使用std::cout 建议可能是一个答案。不幸的是,它不再编译了。
// 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');
对于 C 2.x 和 char8_t
更新
我已经对 u8 序列中的单个元素进行了更多测试。
这确实行不通。 char8_t * 到 printf("%s") 确实有效,但 char8_t 到 printf("%c") 是等待发生的意外。
请看 -- https://wandbox.org/permlink/6NQtkKeZ9JUFw4Sd -- 问题是,按照目前的现状,char8_t 没有实现,char8_t * 是。 -- 让我重复一遍:没有实现的类型来保存来自char8_t * 序列的单个元素。
如果您想要单个 u8 字形,则需要将其编码为 u8 字符串
char8_t const * single_glyph = u8"ア";
目前看来,打印上面的那种肯定的方法是
// works with warnings
std::printf("%s", single_glyph ) ;
要开始阅读这个主题,可能需要这两篇论文
- 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
按这个顺序。
我的主要 DEVENV 是 VisualStudio 2019,带有 MSVC 和 CLANG 8.0.1,与 VS 一起提供。使用 std:c++latest。开发机为WIN10【版本10.0.18362.476】
【问题讨论】:
-
我不希望 C++ 标准委员会将转换说明符添加到
printf,他们将把它留给 C 委员会。 -
C++终于获得了 UTF8 字符类型?哦,喜悦,眼泪......哦,亲爱的,想象一下所有假设
char*和特定LANG的代码。 -
如果您需要跨平台的 UTF-8 支持,使用第三方库。这是此时保持理智的唯一方法。
-
@n.'pronouns'm。 ...好吧,我想我已经过了不归路... :)
-
开个玩笑,新引入的char8_t类型比char类型还差。