下面的 C++ 程序编译没有错误:
void f(char){}
void f(signed char){}
void f(unsigned char){}
int main(){}
同一程序的 wchar_t
版本不会:
void f(wchar_t){}
void f(signed wchar_t){}
void f(unsigned wchar_t){}
int main(){}
错误:‘void f(wchar_t)’的重新定义
void f(signed wchar_t){}
wchar_t
似乎是unsigned
。
为什么重载会出现不一致?
最佳答案
char
都是不同的类型,可以重载
[基础.基础]/1
[...] Plain
char
,signed char
, andunsigned char
are three distinct types, collectively called narrow character types. [...]
wchar_t
也是一个distinct类型,但是不能用signed
或者unsigned
来限定,只能和标准整数一起使用类型。
[dcl.类型]/2
As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule are the following:
[...]
signed
orunsigned
can be combined withchar
,long
,short
, orint
.
[dcl.type.simple]/2
[...] Table 9 summarizes the valid combinations of simple-type-specifiers and the types they specify.
wchar_t
的符号是实现定义的:
[基础.基础]/5
[...] Type
wchar_t
shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type.
关于c++ - 为什么 char 既没有符号也没有符号,而 wchar_t 是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32856481/