I'm thinking of replacing all the instances of safe bool idiom by explicit operator bool
in code which already uses C++11 features (so the fact that older compilers don't recognized explicit conversion operators will not matter), so I'd like to know if it can cause some subtle problems.
Thus, what are all the possible incompatibilities (even the most minute ones) that can be caused by switching from old and dull safe bool idiom to new and shiny explicit operator bool
?
EDIT: I know that switching is a good idea anyway, for the latter is a language feature, well-understood by the compiler, so it'll work no worse than what's in fact just a hack. I simply want to know the possible differences.
If you've used safe-bool conversion incorrectly in your code, only then
explicit operator bool
be incompatible, as it wouldn't allow you to do things incorrectly that easily. Otherwise, it should be just fine without any problem. In fact, even if there is problem, you should still switch toexplicit operator bool
, because if you do so, then you could identify the problem in the usage of the safe-bool conversion.According to this article, some compilers emit inefficient instructions for safe-bool implementation using member function pointer,
Probably the biggest difference, assuming your code is free of bugs (I know, not a safe assumption), will be that in some cases, you may want an implicit conversion to exactly
bool
. Anexplicit
conversion function will not match.