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?
The
char
s are all distinct types and can be overloaded[basic.fundamental] / 1
wchar_t
is also a distinct type, but it cannot be qualified withsigned
orunsigned
, which can only be used with the standard integer types.[dcl.type] / 2
[dcl.type.simple] / 2
The signedness of
wchar_t
is implementation defined:[basic.fundamental] / 5
char
is a distinct type from bothsigned char
andunsigned char
.wchar_t
is yet another distinct type (for type identity purposes), but which has exactly the same properties (size, signedness and alignment) as some other integral type.From ISO 14882:2003, 3.9.1:
There is no such thing as
signed wchar_t
orunsigned wchar_t
. It is not mentioned anywhere in the document.
C++11 $3.9.1/5char
is a fundamental type.wchar_t
evolved as first a library solution (in C), and then became a built in type with an underlying type, corresponding to the type that earlier was used totypedef
it:This explains why you cannot change the signedness of
wchar_t
, but it does not explain why there is achar
type with unspecified signedness.Also, the choice of signed
char
that most compilers default to, is impractical for several reasons. One reason is that the negative values are annoying and generally have to be cast to unsigned in order to compare them. Another reason is that the C character classification functions require non-negative values (except when being passedEOF
). A third reason is that on old magnitude-and-sign or one's complement machines there's one unusable value.There may be some explanation of that in Stroustrup's “The design and evolution of C++”, but I doubt it.
It sounds like frozen history, something that at one point made some kind of sense, for the technology at the time.