Question: Does implicit bool conversions always fall back to attempting implicit conversion to void*
? (If such a conversion function exists for the type). If so, why?
Consider the following short program:
#include <iostream>
class Foo{
public:
operator void*() const
{
std::cout << "operator void*() const" << std::endl;
return 0;
}
};
int main()
{
Foo f;
if(f)
std::cout << "True" << std::endl;
else
std::cout << "False" << std::endl;
return 0;
}
The output of this program is:
operator void*() const
False
meaning, the conversion function to void*
was called.
If we tag an explicit
qualifier in front of the conversion function then the implicit conversion to void*
would fail.
Edit:
It seems many answers are that "null pointers can be converted to false
". I understand this, my question was regarding the "if I can't directly call operator bool()
then I will try conversion to any pointer".