c++ implicit conversion from double to bool is dangerous

Viewed 2552

This is the second time I make a big mistake, by creating a variable of type bool instead of a double. For example consider

double f()
{
    return true;
}
bool something()
{
    return 0.0;
}
double g() 
{
   bool x = 0.0;  // (1)
   if (something()) {
      x = f();  // where f() is a function returning a double
   }
   return x;
}

I think the compiler should tell me that this is bad, but my compiler (g++) does not issue a small warning with -Wall... and it leads to a bug later in tests. Is there some option for gcc/g++ to have warnings (for example on line 1, that is clearly bad) ?

4 Answers
Related