Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is char neither signed or unsigned, but wchar_t is?

The following C++ program compiles without errors:

void f(char){}
void f(signed char){}
void f(unsigned char){}
int main(){}  

The wchar_t version of the same program does not:

void f(wchar_t){}
void f(signed wchar_t){}
void f(unsigned wchar_t){}
int main(){}

error: redefinition of ‘void f(wchar_t)’
void f(signed wchar_t){}

It seems that wchar_t is unsigned.
Why is there an inconsistency in overloading?

like image 548
Trevor Hickey Avatar asked Sep 30 '15 01:09

Trevor Hickey


1 Answers

The chars are all distinct types and can be overloaded

[basic.fundamental] / 1

[...] Plain char, signed char, and unsigned char are three distinct types, collectively called narrow character types. [...]

wchar_t is also a distinct type, but it cannot be qualified with signed or unsigned, which can only be used with the standard integer types.

[dcl.type] / 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

The signedness of wchar_t is implementation defined:

[basic.fundamental] / 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.

like image 106
user657267 Avatar answered Nov 15 '22 15:11

user657267