General topics | ||||||||||||||||
Flow control | ||||||||||||||||
Conditional execution statements | ||||||||||||||||
Iteration statements (loops) | ||||||||||||||||
Jump statements | ||||||||||||||||
Functions | ||||||||||||||||
Function declaration | ||||||||||||||||
Lambda function expression | ||||||||||||||||
inline specifier | ||||||||||||||||
Dynamic exception specifications(until C++17*) | ||||||||||||||||
noexcept specifier(C++11) | ||||||||||||||||
Exceptions | ||||||||||||||||
Namespaces | ||||||||||||||||
Types | ||||||||||||||||
Specifiers | ||||||||||||||||
| ||||||||||||||||
Storage duration specifiers | ||||||||||||||||
Initialization | ||||||||||||||||
Expressions | ||||||||||||||||
Alternative representations | ||||||||||||||||
Literals | ||||||||||||||||
Boolean -Integer -Floating-point | ||||||||||||||||
Character -String -nullptr(C++11) | ||||||||||||||||
User-defined(C++11) | ||||||||||||||||
Utilities | ||||||||||||||||
Attributes(C++11) | ||||||||||||||||
Types | ||||||||||||||||
typedef declaration | ||||||||||||||||
Type alias declaration(C++11) | ||||||||||||||||
Casts | ||||||||||||||||
Memory allocation | ||||||||||||||||
Classes | ||||||||||||||||
Class-specific function properties | ||||||||||||||||
| ||||||||||||||||
Special member functions | ||||||||||||||||
Templates | ||||||||||||||||
Miscellaneous | ||||||||||||||||
General | |||||||||||||||||||||
Literals | |||||||||||||||||||||
Operators | |||||||||||||||||||||
Conversions | |||||||||||||||||||||
|
Many binary operators that expect operands ofarithmetic orenumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called theusual arithmetic conversions.
Contents |
Usual arithmetic conversions are defined as follows:
Applieslvalue-to-rvalue conversion to both operands, the resulting prvalues are used in place of the original operands for the remaining process.
| (since C++11) |
| (since C++26) |
| (since C++23) |
Both operands are converted to a common typeC
. Given the typesT1
andT2
as the promoted type (under the rules of integral promotions) of the operands, the following rules are applied to determineC
:
T1
andT2
are the same type,C
is that type.T1
andT2
are both signed integer types or both unsigned integer types,C
is the type of greaterinteger conversion rank.T1
andT2
is an signed integer typeS
, the other type is an unsigned integer typeU
. Apply the following rules:U
is greater than or equal to the integer conversion rank ofS
,C
isU
.S
can represent all of the values ofU
,C
isS
.C
is the unsigned integer type corresponding toS
.If one operand is of enumeration type and the other operand is of a different enumeration type or a floating-point type, this behavior is deprecated. | (since C++20) (until C++26) |
Everyinteger type has aninteger conversion rank defined as follows:
| (since C++11) |
| (since C++11) |
| (since C++20) |
| (since C++11) |
| (since C++11) |
T1
,T2
, andT3
, ifT1
has greater rank thanT2
andT2
has greater rank thanT3
, thenT1
has greater rank thanT3
.The integer conversion rank is also used in the definition ofintegral promotion.
Everyfloating-point type has afloating-point conversion rank defined as follows:
| (since C++23) |
Floating-point conversion subrankFloating-point types that have equal floating-point conversion ranks are ordered byfloating-point conversion subrank. The subrank forms a total order among types with equal ranks. The types | (since C++23) |
The floating-point conversion rank and subrank are also used to
| (since C++23) |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1642 | C++98 | usual arithmetic conversions might involve lvalues | applies lvalue-to-rvalue conversions first |
CWG 2528 | C++20 | the three-way comparison betweenunsignedchar andunsignedint is ill-formed because of the intermediate integral promotion[1] | determines the common type based on the promoted types, without actually promoting the operands[2] |
CWG 2892 | C++98 | when both operands are of the same floating-point type, the meaning of “no further conversion is needed” was unclear | changed to “no further conversion will be performed” |