This code really needs to compile. The standard has spent a lot of effort to ensure its implementation.
There are several places where the expression "contextually converted to bool". In these places, explicit bool conversions will be called, if available. One such contextual transformation is the if , as in your case.
This language allows the use of explicit operator bool types for conditional checking if(expr) , but you cannot use other things without an explicit conversion. You cannot pass it to a function that accepts a bool ; you cannot return it from a function that returns bool , etc.
All contextual transformations are explicit expressions in language functions. Thus, an explicit operator bool protects you from implicit user conversions, but allows for transforms defined by the language.
source share