0

What will happen when you execute this code snippet?

#include <iostream>

int main() {
   float a = 5.51;
   int b = static_cast<int>(a);
   std::cout << b;
}

Correct answer is:

5 will be printed on standard output, with no compilation warnings generated.

But for me would make more sense to generate compilation warning as precision would be lost. Why not?

Clifford
  • 88,407
  • 13
  • 85
  • 165
pedro_bb7
  • 1,601
  • 3
  • 12
  • 28
  • 7
    The explicit cast tells the compiler you did the conversion intenionally, and therefore there's no need for a warning. Most compiler will issue a warning if you assign a `float` to an `int` **without** a cast. – wohlstad Nov 11 '22 at 12:03
  • 3
    If you do a direct assignment with implicit conversion (i.e. `int b = a;`) then most compilers should give a warning (at least if you enable more warnings, which is always a good idea). But `static_cast(a)` is an *explicit* conversion, that makes the compiler think you made it intentionally and that you really know what you're doing. – Some programmer dude Nov 11 '22 at 12:03
  • 2
    You order a pizza. And when the Waiter arrives, you **explicitly** tell him he should make a Pizza shake instead. After the waiter comes back with a Pizza shake, you complain you weren't warned that your pizza becomes a pizza shake. – Raildex Nov 11 '22 at 12:26
  • The whole point of the cast is to lose precision without a nitpicking compiler pestering you about it. – molbdnilo Nov 11 '22 at 12:37
  • `float a = 5.51;` loses precision (double converted to float). `int b = static_cast(a);` explicitly tells the compiler to lose precision. You could make your own conversion routine that can throw an exception if the conversion would lose precision that you care about. – Eljay Nov 11 '22 at 13:32
  • 1
    @Eljay -- `5.51` loses precision, too: text converted to double! But that's a rathole... – Pete Becker Nov 11 '22 at 15:09

2 Answers2

0

I think it depends on compiler level of warning. In my case with Visual Studio 2022 and level warnings set to lvl 4, I get the following warning:

Warning C4305 'init': truncation from 'double' to 'float'.

Bob
  • 9
  • 2
  • 1
    That warning message is about initializing the `float` variable `a` using the `double` value `5.51`. Not about the conversion of the `float` value inside the variable `a` to an `int` value for the initialization of the variable `b`. – Some programmer dude Nov 11 '22 at 12:08
  • 1
    Same for clang with `-Wimplicit-float-conversion` enabled. But I think both OP and the author of the quiz are referring to the `static_cast` which should not generate a warning. – perivesta Nov 11 '22 at 12:08
  • @perivesta think in my workplace environment it was set to enabled and tricked me on this question. – pedro_bb7 Nov 11 '22 at 12:12
0

As commented by @wohlstad:

The explicit cast tells the compiler you did the conversion intenionally, and therefore there's no need for a warning. Most compiler will issue a warning if you assign a float to an int without a cast.

pedro_bb7
  • 1,601
  • 3
  • 12
  • 28