This question extends Why use !! when converting int to bool?.
I thought I was being cool when I did something like:
bool hasParent() {
return this->parentNode;
}
Where this->parentNode is NULL when there is no parent node.
But I’m getting:
warning C4800: ‘Node *’ : forcing value to bool ‘true’ or ‘false’ (performance warning)
Even with a (bool) cast, the warning still doesn’t go away.
What’s the deal, yo? Why is that a performance warning? I thought it’d be less efficient to write something like:
bool hasParent() {
if (this->parentNode)
return true;
else
return false;
}
But the second version generates no warnings and the compiler seems a lot happier. Which is faster though?
There’s a discussion on Microsoft Connect about this (What is the performance implication of converting to bool in C++?). The example given to Microsoft is:
And Microsoft’s response (from the developer responsible for the warning) is:
So basically the MS developer seems to be saying that if you want to ‘cast’ an
inttoboolyou should more properly do it by using “return this->parentNode != 0” instead of an implicit or explicit cast.Personally, I’d be interested to know more about what kind of bugs the warning uncovers. I’d think that this warning wouldn’t have a whole lot of value.