I had a function: void foo(bool boolParam = true)
And I changed it to: void foo(const char* charParam, bool boolParam = true)
To avoid searching I just compiled the code hoping that the compiler will give an error (or at least an warning) where the function was called because of wrong parameter type, but instead of this the compiler silently converted false to NULL
and compiled everything without error or warning. Is this behavior correct? I know that false and NULL
are both 0, but I think the compiler should give at least some warning message...
You could leave your original function unimplemented:
void foo(bool boolParam = true);
void foo(const char* charParam, bool boolParam = true)
{
// do stuff
}
Now whenever you call foo()
, foo(true)
, and foo(false)
it will cause a compile error. However, foo(NULL)
won't compile either because NULL and false are ambiguous (and then we are back to square one...).
The behavior is entirely correct, because (as you note) the conversion from false
(a valid null pointer constant) to pointer is implicit. Try a std::string
instead.
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