Why is char not signed or not signed, but wchar_t?

The following C ++ program compiles without errors:

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

The wchar_t version does not have the same program:

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

error: override "void f (wchar_t)
void f (signed by wchar_t) {}

Wchar_t seems to be unsigned .
Why is there inconsistency in overloading?

+7
c ++ implicit-conversion function-overloading wchar-t
source share
3 answers

char - all different types and can be overloaded

[basic.fundamental] / 1

[...] Regular char , signed char and unsigned char are three different types, collectively called narrow character types. [...]

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

[dcl.type] / 2

Typically, no more than one type specifier is allowed in a full decl-specifier-seq declaration or in type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule include the following:

[...]

signed or unsigned can be combined with char , long , short or int .

[dcl.type.simple] / 2

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

enter image description here

The wchar_t is determined by the implementation:

[basic.fundamental] / 5

[...] The wchar_t type must have the same size, signature and alignment (3.11) as one of the other integral types, called its base type.

+9
source share

char is a distinct type from both signed char and unsigned char . wchar_t is another separate type (for type identification purposes), but which has exactly the same properties (size, signature, and alignment) as some other integral type.

From ISO 14882: 2003, 3.9.1:

The regular char , signed char and unsigned char are three different types.

(...)

The wchar_t type is a separate type whose values โ€‹โ€‹can represent different codes for all members of the largest extended character set among supported locales (22.1.1). The wchar_t type must have the same size, signature, and alignment requirements (3.9) as one of the other integral types, called its base type.

There is no such thing as signed wchar_t or unsigned wchar_t . It is not mentioned anywhere in the document.

+4
source share

char is a fundamental type. wchar_t evolved as the first library solution (in C), and then became an inline type with a base type corresponding to the type previously used for typedef it:

C ++ 11 $ 3.9.1 / 5

" The wchar_t type shall have the same size, signature and alignment requirements (3.11) as one of the other integral types, called its base type.

This explains why you cannot change the wchar_t signature, but does not explain why there is a char type with an undefined signature.


In addition, choosing a signed char , which is used by most compilers by default, is impractical for several reasons. One reason is that negative values โ€‹โ€‹are annoying and should usually be distinguished without sign in order to compare them. Another reason is that the character classification functions of C require non-negative values โ€‹โ€‹(unless they are transmitted by EOF ). The third reason is that at old stations with a magnitude and sign or on one addition there is one unsuitable value.

There may be some explanation in โ€œStroustrupโ€ โ€œDesign and evolution of C ++โ€, but I doubt it.

It sounds like a frozen story, something that at some point made some sense for the technology at that time.

+4
source share

All Articles