为什么我得到的是数字而不是Unicode字符
Why do I get a number instead of a Unicode character?
我写了这个代码:
#include <iostream>
int main()
{
std::wcout << 'u00E1' << std::endl;
}
但当使用GCC 4.8.1编译时,它会输出50081
。
我可能做错了什么,但我肯定不会期望输出一个数字。怎么回事?
我认为这是g++中的一个bug。'u00E1'
的类型是char
,但g++将其视为int
。clang++做对了。
考虑一下这个相关的程序(使用重载的type_of
函数来检测文字的类型):
#include <iostream>
const char *type_of(char) { return "char"; }
const char *type_of(int) { return "int"; }
int main()
{
std::cout << "type_of('x') = " << type_of('x') << "n";
std::cout << "type_of('xy') = " << type_of('xy') << "n"; // line 9
std::cout << "type_of('u00E1') = " << type_of('u00E1') << "n"; // line 10
std::cout << "type_of('u0100') = " << type_of('u0100') << "n"; // line 11
}
当我用g++4.7.2编译这个时,我得到了以下警告:
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]
这个输出:
type_of('x') = char
type_of('xy') = int
type_of('á') = int
type_of('Ā') = int
使用clang++3.0,我只收到两个警告:
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
字符文字'u00E1'
只有一个c-字符序列,它恰好是通用字符名。因此它属于char
类型,但g++错误地将其视为int
类型的多字符常量。clang++正确地将其视为char
类型的普通字符文字。
这样一个值在char
范围之外的字符文字的值是实现定义的,但它仍然是char
类型。
由于您正在编写std::wcout
,您可能想要一个宽字符的文字:L'u00E1'
,它的类型为char_t
,而不是'u00E1'
,它(如果编译器处理正确)的类型为int
。
这似乎是编译器错误。
根据标准(2.14.3/1),'u00E1'
是一个普通的字符文字(它没有u
、U
或L
前缀),它包含一个c-char(这是一个通用字符名),因此它具有类型char
。
因此std::wcout << 'u00E1'
应该使用operator<<(char)
并打印单个字符。
相反,它采用通用字符名,将其转换为UTF-8编码序列,并获得多字符文字"\xC3\xA1",这是一个值为50081:的int
'u00E1' -> 'xC3xA1' -> 50081
相关文章:
- 如何存储 unicode 字符并将其输出到文件?
- C++:打印 Unicode 字符
- 在 Windows 中使用 boost::p rogram_options 从命令行参数读取 Unicode 字符
- 获取Unicode字符C++的十进制值
- 用wchar_t处理 unicode 字符好吗?它不会引起任何问题吗?
- C++:如何将 unicode 字符打印到文本文件
- 动态计算 unicode 字符
- 如何将整数转换为 unicode 字符
- C++20 中的严格别名规则是否允许标准 c++ unicode 字符和下划线类型之间"reinterpret
- MessageBox打印来自TCHAR缓冲区的额外Unicode字符
- 如何将小数NCR转换为Unicode字符(C )
- SQLGetPrivateProfileString 错误地读取 Unicode 字符
- 将 Unicode 字符/字符串写入文件
- 将 Unicode 字符存储在.txt文件中的新行中
- 从终端 C++ 中的字符串中打印出 unicode 字符
- Unicode 字符分类与 boost::locale
- 字符数组中的滤波器unicode字符
- Unicode 字符问题/转换参数
- 从包含Unicode字符的文件中读取
- 从ASCII到Unicode字符代码的转换(FreeType2)