为什么char不带符号或不带符号,而wchar_t是带符号的? [英] Why is char neither signed or unsigned, but wchar_t is?
问题描述
以下C ++程序可正确编译:
The following C++ program compiles without errors:
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)"
无效f(signed wchar_t){}
error: redefinition of ‘void f(wchar_t)’
void f(signed wchar_t){}
似乎wchar_t
是unsigned
.
为什么超载不一致?
It seems that wchar_t
is unsigned
.
Why is there an inconsistency in overloading?
推荐答案
char
都是不同的类型,可以重载
The char
s are all distinct types and can be overloaded
[基本原理]/1
[...]普通
char
,signed char
和unsigned char
是三种不同的类型, 统称为窄字符类型. [...]
[...] Plain
char
,signed char
, andunsigned char
are three distinct types, collectively called narrow character types. [...]
wchar_t
也是一种独特的类型,但是不能用signed
或unsigned
限定,它们只能与标准整数类型一起使用.
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
[dcl.type] / 2
通常,最多允许一个 type-specifier 声明的完整 decl-specifier-seq 或 type-specifier-seq 或 trailing-type-specifier-seq .唯一的例外 该规则如下:
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
或unsigned
可以与char
,long
,short
或int
组合.
signed
or unsigned
can be combined with char
, long
, short
, or int
.
[dcl.type.simple]/2
[dcl.type.simple] / 2
[...]表9总结了简单类型说明符及其指定类型的有效组合.
[...] Table 9 summarizes the valid combinations of simple-type-specifiers and the types they specify.
wchar_t
的签名是由实现定义的:
The signedness of wchar_t
is implementation defined:
[基本知识]/5
[...]类型
wchar_t
必须具有相同的大小,符号和对齐方式 要求(3.11)作为其他整数类型之一,称为 基础类型.
[...] 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.
这篇关于为什么char不带符号或不带符号,而wchar_t是带符号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!