c++language-lawyerqa-c

Is there an implicit type promotion in "float = float - float"?


We are using QA-C for MISRA C++ conformance, but the tool spews out an error for code like this:

float a = foo();
float b = bar();
float c = a - b;

As far as I understand, this has no implicit type promotion as everything will happen in float-sized chunks, but the tool tells me that the subtraction causes one. Is there any situation where there might be implicit promotion?


Solution

  • There is no implicit promotion involved here.

    When conversions involving binary operators are involved, they are called usual arithmetic conversions.

    From C++ standard, [expr]/11:

    11 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:
    ...
    (11.4) — Otherwise, if either operand is float, the other shall be converted to float.

    Since both operands are float in your example, there is no such conversion or promotion.
    So this could be a false positive from the tool.