简体   繁体   English

C ++类型等级(有符号/无符号int的类型转换)

[英]C++ type rank (type conversion for signed/unsigned int)

I'm confused about type conversion between two signed/unsigned ints. 我对两个有符号/无符号整数之间的类型转换感到困惑。 Why do signed and unsigned integers have the same type rank? 为什么有符号和无符号整数具有相同的类型等级?

I searched "c++ int type rank" and found and answer to the queston What does rank mean in relation to type conversion? 我搜索了“ c ++ int type rank”,找到并回答了问题。rank 与类型转换有关是什么意思? that mentioned: 提到:

From C++11 standard (draft n3337) §5/9:: 根据C ++ 11标准(n3337草案)§5/ 9 ::

— If both operands have the same type, no further conversion is needed. —如果两个操作数具有相同的类型,则无需进一步转换。

— Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank. —否则,如果两个操作数都具有符号整数类型或都具有无符号整数类型,则应将具有较小整数转换等级的操作数转换为具有较大等级的操作数的类型。

— Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type. —否则,如果具有无符号整数类型的操作数的秩大于或等于另一个操作数的类型的秩,则应将具有符号整数类型的操作数转换为具有无符号整数类型的操作数的类型。

— Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type. —否则,如果带符号整数类型的操作数的类型可以表示无符号整数类型的操作数的所有值,则应将无符号整数类型的操作数转换为带符号整数类型的操作数的类型。

— Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type. —否则,两个操作数均应转换为与带符号整数类型的操作数类型相对应的无符号整数类型。

But I cannot find anything about signed/unsigned int type ranks in ISO/IEC 14882:2011. 但是我在ISO / IEC 14882:2011中找不到关于有符号/无符号int类型等级的任何信息。

Is there an official description for signed/unsigned int type conversion in ISO/IEC Spec? 在ISO / IEC规范中是否有关于有符号/无符号int类型转换的正式描述?

Henri Menke found the documentation at http://eel.is/c++draft/conv.rank but I have included the relevant sections below. 亨利·门科(Henri Menke)在http://eel.is/c++draft/conv.rank上找到了该文档,但我在下面提供了相关部分。

1.4 The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type. 1.4任何无符号整数类型的等级应等于相应的有符号整数类型的等级。

1.6 The rank of char shall equal the rank of signed char and unsigned 1.6字符的等级应等于签名字符和未签名字符的等级

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM