C++ 在C中,如何确定两种类型是否相同?

C++ 在C中,如何确定两种类型是否相同?,c++,c,C++,C,从 算术类型的各种组合中存在类型说明符可能表示也可能不表示不同的类型。例如,有符号int的类型与int相同,但用作位字段的类型时除外;但是字符、有符号字符和无符号字符是不同的类型。 int和char在类型标识方面存在这种差异的原因是什么 我猜同样的规则也适用于C++。因此,从C++ 11中也添加了C++标签的问题:(来自3.9段落的段落) 纯字符、有符号字符和无符号字符是三种不同的类型,统称为窄字符类型 有五种标准的有符号整数类型:“有符号字符”、“短整数”、“整数”、“长整数”和“长整数”。

算术类型的各种组合中存在类型说明符可能表示也可能不表示不同的类型。例如,有符号int的类型与int相同,但用作位字段的类型时除外;但是字符、有符号字符和无符号字符是不同的类型。

int和char在类型标识方面存在这种差异的原因是什么

我猜同样的规则也适用于C++。因此,从C++ 11中也添加了C++标签的问题

:(来自3.9段落的段落)

纯字符、有符号字符和无符号字符是三种不同的类型,统称为窄字符类型

有五种标准的有符号整数类型:“有符号字符”、“短整数”、“整数”、“长整数”和“长整数”。
对于每种标准有符号整数类型,都存在相应的(但不同的)标准无符号整数

整数类型:“无符号字符”、“无符号短整数”、“无符号整数”、“无符号长整数”, “unsigned long int”和“unsigned long int”,它们各自占用相同的存储量并具有相同的 对齐要求(3.11)为相应的有符号整数类型47

类型wchar\u t是一个不同的类型…类型char16\u t和char32\u t表示不同的类型

bool类型的值为true或false。49[注意:没有带符号、无符号、短或长的值 布尔类型或值。-结束注释]

§7.1.6.2列出了一个表格,其中的关键字导致了以下类型:

type-name          the type named
simple-template-id the type as defined in 14.2
char               “char”
unsigned char      “unsigned char”
signed char        “signed char”
char16_t           “char16_t”
char32_t           “char32_t”
bool               “bool”
unsigned           “unsigned int”
unsigned int       “unsigned int”
signed             “int”
signed int         “int”
int                “int”
unsigned short int “unsigned short int”
unsigned short     “unsigned short int”
unsigned long int  “unsigned long int”
unsigned long      “unsigned long int”
unsigned long long int “unsigned long long int”
unsigned long long “unsigned long long int”
signed long int    “long int”
signed long        “long int”
signed long long int “long long int”
signed long long   “long long int”
long long int      “long long int”
long long          “long long int”
long int           “long int”
long               “long int”
signed short int   “short int”
signed short       “short int”
short int          “short int”
short              “short int”
wchar_t            “wchar_t”
float              “float”
double             “double”
long double        “long double”
void               “void”
auto               placeholder for a type to be deduced
decltype(expression) the type as defined below
至于为什么
char
是wierd,我听说最初有一些编译器使
char
无符号(在
unsigned
类型被构思出来之前?)。C++标准想要纠正这个问题,但不想破坏现有代码,所以他们定义了<代码> char < /C>实现,并添加了代码>符号char < /C>作为单独的类型。

< >从C++ 11:(席3.9中的不同段落)< /P> 纯字符、有符号字符和无符号字符是三种不同的类型,统称为窄字符类型

有五种标准的有符号整数类型:“有符号字符”、“短整数”、“整数”、“长整数”和“长整数”。
对于每种标准有符号整数类型,都存在相应的(但不同的)标准无符号整数

整数类型:“无符号字符”、“无符号短整数”、“无符号整数”、“无符号长整数”, “unsigned long int”和“unsigned long int”,它们各自占用相同的存储量并具有相同的 对齐要求(3.11)为相应的有符号整数类型47

类型wchar\u t是一个不同的类型…类型char16\u t和char32\u t表示不同的类型

bool类型的值为true或false。49[注意:没有带符号、无符号、短或长的值 布尔类型或值。-结束注释]

§7.1.6.2列出了一个表格,其中的关键字导致了以下类型:

type-name          the type named
simple-template-id the type as defined in 14.2
char               “char”
unsigned char      “unsigned char”
signed char        “signed char”
char16_t           “char16_t”
char32_t           “char32_t”
bool               “bool”
unsigned           “unsigned int”
unsigned int       “unsigned int”
signed             “int”
signed int         “int”
int                “int”
unsigned short int “unsigned short int”
unsigned short     “unsigned short int”
unsigned long int  “unsigned long int”
unsigned long      “unsigned long int”
unsigned long long int “unsigned long long int”
unsigned long long “unsigned long long int”
signed long int    “long int”
signed long        “long int”
signed long long int “long long int”
signed long long   “long long int”
long long int      “long long int”
long long          “long long int”
long int           “long int”
long               “long int”
signed short int   “short int”
signed short       “short int”
short int          “short int”
short              “short int”
wchar_t            “wchar_t”
float              “float”
double             “double”
long double        “long double”
void               “void”
auto               placeholder for a type to be deduced
decltype(expression) the type as defined below

至于为什么
char
是wierd,我听说最初有一些编译器使
char
无符号(在
unsigned
类型被构思出来之前?)。C++标准想要纠正这个问题,但不想破坏现有代码,所以他们定义了<代码> char < /C>实现,并添加了代码>符号char < /C>作为单独的类型。

原来C没有“<代码>未签名< /Cord>”类型,因此默认情况下,char >。由于PDP-11使用8位字节和ASCII字符集,

char
对象为:

字符(声明的,以下称为,
char
)从ASCII集合中选择;它们占据8位字节最右边的7位。也可以将
char
s解释为有符号,2的补码为8位 数字

unsigned
类型在一定程度上是为了阻止在无符号算术中使用指针:

[…]字符指针和无符号整数的算术属性的相似性使人们很难抵制识别它们的诱惑。添加无符号类型是为了使无符号算术可用,而不会将其与指针操作混淆

char
进行签名的问题在于,为了对
char
值执行算术运算,必须对它们进行签名。这在PDP-11上是有效的,它有一个SXT指令,但是(至少,直到1996年ARM架构的第4版发布):

[…]在ARM指令集中找不到单个“从内存加载字符和符号扩展”。这就是为什么出于性能原因,我所知道的每个编译器都会使用默认的字符类型,在x86上签名,但在ARM上未签名。[……]


因此,当C被标准化时,委员会可以选择要么使
char
签名,从而迫使ARM实现变得低效,要么反映现有实践,定义
char
实现的签名性。

最初C没有
未签名的
类型,因此默认情况下,
char
已签名。由于PDP-11使用8位字节和ASCII字符集,
char
对象为:

字符(声明的,以下称为,
char
)从ASCII集合中选择;它们占据8位字节最右边的7位。也可以将
char
s解释为有符号,2的补码为8位 数字

unsigned
类型在一定程度上是为了阻止在无符号算术中使用指针:

[…]字符指针和无符号整数的算术属性的相似性使人们很难抵制识别它们的诱惑。添加无符号类型是为了在不混淆无符号算术的情况下使其可用