[cfe-dev] warning with conditional operator and printf, is this a bug ?
David Blaikie
dblaikie at gmail.com
Thu Nov 3 22:34:06 PDT 2011
> This is a test case, not sure if its a bug with printf.
I don't believe this is a bug, no.
> This seems strange because both operands are the same type (the one that is
> expected).
What you're seeing here are the Usual Arithmetic Conversions. [expr] 5\9:
Many binary operators that expect operands of arithmetic or
enumeration 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 the usual arithmetic conversions, which are
defined as follows:
— If either operand is of scoped enumeration type (7.2), no
conversions are performed; if the other
operand does not have the same type, the expression is ill-formed.
— If either operand is of type long double, the other shall be
converted to long double.
— Otherwise, if either operand is double, the other shall be
converted to double.
— Otherwise, if either operand is float, the other shall be
converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on
both operands.59 Then the following
rules shall be applied to the promoted operands:
— If both operands have the same type, no further conversion is needed.
and [conv.prom] 4.5\1 says:
A prvalue of an integer type other than bool, char16_t, char32_t, or
wchar_t whose integer conversion
rank (4.13) is less than the rank of int can be converted to a prvalue
of type int if int can represent all
the values of the source type; otherwise, the source prvalue can be
converted to a prvalue of type unsigned
int.
which means your uint16s get promoted to ints at the drop of a hat &
the type of the conditional expression in the printf is actually int,
not uint16_t.
- David
More information about the cfe-dev
mailing list