I am learning C++ recently and I noticed an example on cppreference, part of which goes like this:
struct B
{
explicit B(int) { }
explicit operator bool() const { return true; }
};
int main()
{
B b2(2); // OK: direct-initialization selects B::B(int)
if (b2) ; // OK: B::operator bool()
}
The introduction to implicit conversions tells me "when the expression is used in an if statement or a loop" the result of this expression( b2 ) will be converted into bool
type implicitly.
Also, the introduction to explicit specifier tells me if "a conversion function is explicit, it cannot be used for implicit conversions".
Since b2 will be converted implicitly in if(b2)
, and the conversion function is explicit
, how comes if(b2)
is ok?