Reputation: 1466
I feel like 6.3.1.3 of standard is already included in 6.3.1.8
but reading:
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
It is not mentioned this case there. So is 6.3.1.3 for integer promotions only and when there is casting what is the order of conversion. Is it promotion and then casting or casting then promotions or only casting .
Upvotes: 0
Views: 236
Reputation: 241671
There are a number of circumstances under which a C compiler will insert code to convert one datatype to another. These include explicit conversions performed by the cast operator, and a variety of implicit conversions. Both of these conversion contexts are covered by §6.3.1:
- Several operators convert operand values from one type to another automatically. This subclause specifies the result required from such an implicit conversion, as well as those that result from a cast operation (an explicit conversion).
§6.3.1.3 explains how the conversions are performed, without specifying when the conversions are performed. The same procedure is used both for explicit and implicit conversions; to perform a conversion, it is only needed to know the original type and the desired type.
§6.3.1.8 explains how the destination type is selected in some common contexts, without specifying the algorithm for the conversion. So the two clauses are entirely complementary. One describes how a conversion is done, and the other describes which conversion is done.
§6.3.1.8 applies to certain arithmetic operators whose operands must be of the same type. (As noted, this clause doesn't apply to all arithmetic operators and you have to read the description of each individual operator to see the exceptions.) It should be noted that the algorithm to select the target types for both operands only results in "widening" conversions. The target type is always as wide as or wider than the original type. It is possible that a signed integer will be converted to an unsigned integer of the same width, but an unsigned type is only converted to a signed type when the signed type is wide enough to represent all values of the unsigned type.
The implementation of signed to unsigned conversions is precisely defined without implementation-defined behaviour in §6.3.1.3p2;. Thus, paragraph §6.3.1.3p3 will never apply to the conversions required by §6.3.1.8. This is also true of the integer promotions defined in §6.3.1.1p2, which can be triggered by the algorithm described in §6.3.1.8.
In other contexts in which automatic or explicit conversion takes place, narrowing conversions may be requested; if the target of a narrowing conversion is signed, §6.3.1.3p3 may apply.
Upvotes: 2