Consider the following code:
void f(int x)
{
std::cout << x << std::endl;
}
int main()
{
double x = 1.5;
f(x);
return 0;
}
This compiles and runs without any warnings (using -Wall), and therefore hides a dangerous implicit cast from double to int. The compiler will catch the cast if the function is called with a literal, i.e.
f(1.5)
but this isn't all that useful. Why don't these compilers give warnings for this cast? I'm on OSX 10.8.3 with gcc-4.2.1 and clang-425.0.28.
For posterity:
In order to avoid implicit conversions, use the -Wconversion flag (it's not included in -Wall). Clang actually includes -Wconversion in the -Weverything flag, but this flag enables quite a bit more warnings than most users are accustomed to.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With