9
votes

From C++11 standard (draft 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.

What does rank mean in this context?
Surely it's not referring to std::rank,
as that has to do with the number of dimensions in an array...

In terms of integral types and floating point types, I think it refers to their potential sizes.

The C++ Standard guarantees that:

1 == sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <=  sizeof(long long)  

am I right to assume then that the ranks are then:

RankType
1 char
2 short
3 int
4 long
5 long long
...

I haven't been able to find a list anywhere describing the level of rank for each type.

1
See 4.13/p1 ([conv.rank]).0x499602D2
@0x499602D2 Yup, that's what I was looking for.Trevor Hickey
In the C spec: 6.3.1.1 Boolean, characters, and integersRoland

1 Answers

9
votes

The 4.13 section says that

Every integer type has an integer conversion rank defined as follows:

— No two signed integer types other than char and signed char (if char is signed) shall have the same rank, even if they have the same representation.

— The rank of a signed integer type shall be greater than the rank of any signed integer type with a smaller size.

— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type

— The rank of any standard integer type shall be greater than the rank of any extended integer type with the same size.

— The rank of char shall equal the rank of signed char and unsigned char.

— The rank of bool shall be less than the rank of all other standard integer types.

— The ranks of char16_t, char32_t, and wchar_t shall equal the ranks of their underlying types (3.9.1).

— The rank of any extended signed integer type relative to another extended signed integer type with the same size is implementation-defined, but still subject to the other rules for determining the integer conversion rank.

— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 shall have greater rank than T3.