C++ 为什么我得到的是数字而不是Unicode字符?

C++ 为什么我得到的是数字而不是Unicode字符?,c++,C++,我写了这段代码: #include <iostream> int main() { std::wcout << '\u00E1' << std::endl; } #包括 int main() { std::wcout这似乎是编译器错误 根据标准(2.14.3/1)“\u00E1”是一种普通字符文字(它没有u、u或L前缀),它包含一个c字符(这是一个通用字符名),因此它具有类型char 所以std::wcout我认为这是g++中的一个bug。'\u00

我写了这段代码:

#include <iostream>

int main()
{
   std::wcout << '\u00E1' << std::endl;
}
#包括
int main()
{

std::wcout这似乎是编译器错误

根据标准(2.14.3/1)
“\u00E1”
是一种普通字符文字(它没有
u
u
L
前缀),它包含一个c字符(这是一个通用字符名),因此它具有类型
char


所以
std::wcout我认为这是g++中的一个bug。
'\u00E1'
的类型是
char
,但g++将其视为
int
。clang++正确地做到了这一点

考虑这个相关程序(使用重载的
type\u
函数来检测文本的类型):

这个输出:

type_of('x')  = char
type_of('xy') = int
type_of('á')  = int
type_of('Ā')  = int
type_of('x')  = char
type_of('xy') = int
type_of('á')  = char
type_of('Ā')  = char
使用clang++3.0,我只收到两个警告:

c.cpp:9:47: warning: multi-character character constant [-Wmultichar]
c.cpp:10:52: warning: multi-character character constant [-Wmultichar]
c.cpp:11:52: warning: multi-character character constant [-Wmultichar]
c.cpp:9:47: warning: multi-character character constant [-Wmultichar]
   std::cout << "type_of('xy') = " << type_of('xy') << "\n";
                                              ^
c.cpp:11:52: warning: character unicode escape sequence too long for its type
   std::cout << "type_of('\u0100')  = " << type_of('\u0100') << "\n";
字符文本
'\u00E1'
只有一个c-char序列,这恰好是一个通用字符名,因此它属于
char
类型,但g++错误地将其视为
int
类型的多字符常量。clang++正确地将其视为
char
类型的普通字符文本

这种字符文本的值超出
char
的范围是实现定义的,但它仍然是
char
类型


由于您要编写的是
std::wcout
,您可能需要一个宽字符文本:
L'\u00E1'
,它是
char\u t
类型,而不是
'\u00E1'
,后者(如果您的编译器处理正确)是
int

-1您正在编译一个不同的codeOK@Abyx:问题是这样的“为什么是数字?这太疯狂了"。难道
2.14.3/5
不需要这样的转换吗?@LightnessRacesinOrbit:它仍然是
char
类型@KeithThompson:所以转换发生得太晚了,无法有效地为
2.14.3/1
生成多个c字符来启动吗?在我删除我的答案之前,真诚地要求仔细检查一下。@LightnessRacesinOrbit:我看不到mention是2.14.3/5中的一个转换。很难说编码如何应用于单个字符,而不是字节序列。我的意思是我们有一个单一的代码点,我们可以将其编码为一个字节序列,但这里我们应该用
char
int
对其进行编码。
c.cpp:9:47: warning: multi-character character constant [-Wmultichar]
   std::cout << "type_of('xy') = " << type_of('xy') << "\n";
                                              ^
c.cpp:11:52: warning: character unicode escape sequence too long for its type
   std::cout << "type_of('\u0100')  = " << type_of('\u0100') << "\n";
type_of('x')  = char
type_of('xy') = int
type_of('á')  = char
type_of('Ā')  = char