10

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?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • 13
    Would you mind adding the exact error that it gives you? – Max Langhof Apr 26 '19 at 07:49
  • 1
    Consider also this [Q&A](https://stackoverflow.com/a/8073298/4944425), maybe, maybe the tool you are using is misinterpreting what's stated in the second part of that answer. – Bob__ Apr 26 '19 at 08:19
  • Possible duplicate of [How is float variable auto-promoted to double type?](https://stackoverflow.com/questions/12118738/how-is-float-variable-auto-promoted-to-double-type) – phuclv Apr 26 '19 at 08:29
  • The error is in Japanese, and it's on a colleague's PC, but he's just gone home for the day... – Ken Y-N Apr 26 '19 at 08:36
  • Standard (which is very clear, see answer by P.W) aside, from a purely logical point of view alone, this cannot be the case. Otherwise, everything would be promoted to `double`, eventually. It wouldn't even make sense to have a `float` or `int` type any more. While a compiler would in principle "allowed" to do that (no change in EOB), performance is nevertheless a key expectation. Promotions cost additional resources (though sometimes, by accident, they're free), and they therefore only ever occur if and when they are necessary (in a sense that otherwise there's a chance to lose information). – Damon Apr 26 '19 at 11:37
  • 3
    @Damon that's a non-sequitur; `short` is promoted to `int` in this case and yet it makes sense to have `short` – M.M Apr 26 '19 at 12:06
  • @M.M: Excuse me? `Integral promotion shall be performed on both. Then [...] if both operands have the same type, no further conversion is needed.` Where do you get from that `short` is ever needlessly converted to `int` in such a context? Though it's true that `short` indeed pretty much doesn't make any sense. There's no guarantee that it's any shorter -- but if you want that, there's explicit types that guarantee smaller sizes. And an `int` can do everything that a `short` can do, usually as fast or faster. – Damon Apr 26 '19 at 12:17
  • 1
    @Damon "integral promotion" takes `short` to `int` (on common systems) – M.M Apr 26 '19 at 12:19
  • @M.M.: Now, _that_ is a non-sequitur. Because integral promition _does not_ promote `short` to `int` _de lege lata_. It merely _allows_ for that to happen for types with a rank lower than `int`, and with some additional constraints (such that e.g. `bool` must strictly convert to either 0 or 1). It is allowable for an implementation to do it, but as long as the observable behavior is the same, an implementation can already do whatever it wants anyway. Definitively "is allowable" is not the same as "will happen", or "does happen". – Damon Apr 26 '19 at 12:27
  • 3
    @Damon you already quoted the text that says "integral promotion **shall** be performed on both" (not "is allowed to happen" or whatever). – M.M Apr 26 '19 at 12:31
  • @M.M: Oh ffs... why do we have to have such a pointless discussion. Integral promotion **shall** happen, yes. But integral promotion by itself merely **allows** types being promoted. (I'm out). – Damon Apr 26 '19 at 13:20
  • 1
    @Damon: See for instance https://stackoverflow.com/a/5563131/2718186, which states that anything lower-ranked than an `int` (e.g. a `short`) is promoted to an `int` before arithmetic operations are applied. – MicroVirus Apr 26 '19 at 13:27
  • 3
    Please provide the *actual error message* and *actual code* that produces that error message. – Yakk - Adam Nevraumont Apr 26 '19 at 13:54

1 Answers1

15

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.

P.W
  • 26,289
  • 6
  • 39
  • 76