c++ - 为什么 char 既没有符号也没有符号,而 wchar_t 是?

标签 c++ implicit-conversion overloading wchar-t

下面的 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, and unsigned 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 or unsigned can be combined with char, long, short, or int.

[dcl.type.simple]/2

[...] Table 9 summarizes the valid combinations of simple-type-specifiers and the types they specify.

enter image description here

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/

相关文章:

c++ - 打印任何 vector 的内容

c++ - boost 变体 : Get pointer of generic class providing a variant

scala - Scala 中是否可以同时具有隐式 Ordering[Option[T] 和 Ordered[Option[T]] ?

c++ - 以下哪一种函数类型不能被重载?

c - C中的函数重载

Scala重载高阶函数导致类型错误

c++ - 如何使用可变参数模板初始化指针?

c++ - 替换 av_read_frame() 以减少延迟

scala - 为什么从 Long 到 RichLong 的隐式转换未应用在需要 RichLong 父类(super class)型的地方?

c++ - 有没有办法停止隐式指针转换为 void *