I'm using native C++ with Visual Studio 2019 16.11.8. I don't understand this: whis can the false keyword be used as NULL (or nullptr)? Below the test code:
bool test(bool* pb)
{
if (NULL != pb)
return *pb;
else
return false;
}
void main()
{
test(false); // compiles (not what I was expecting)
test(true); // won't compile: error C2664: 'test' : cannot convert parameter 1 from 'bool' to 'bool *'
}
I have tried online compilers tool, and they do not accept the
test(false);
line, which sounds to me what the normal behavior should be.
In addition, this behavior can cause issues with overloaded methods. i.e., you have a test(bool* pb) method and an overloaded version with a pointer: test(int* pv)
bool test(bool* pb)
{
if (NULL != pb)
return *pb;
else
return false;
}
bool test(int* pb)
{
if (NULL != pb)
return true;
else
return false;
}
void main()
{
test(false); // won't compile anymore: error C2668: 'test' : ambiguous call to overloaded function
test(true); // won't compile: error C2664: 'test' : cannot convert parameter 1 from 'bool' to 'bool *'
}
BTW I'm also using VS2012, which has the same behavior with the same test codes.