According to cppreference.com an explicit conversion function cannot be used for implicit conversions. As an example they have this:
struct B
{
explicit B(int) { }
explicit B(int, int) { }
explicit operator bool() const { return true; }
};
int main()
{
...
if (b2) ; // OK: B::operator bool()
...
}
I would have thought that 'if (b2)' was an implicit conversion and therefore not able to use the explicit conversion function. So what would be an example of an implicit conversion that wouldn't be allowed?
In the following contexts, the type
bool
is expected and the implicit conversion is performed if the declarationbool t(e);
is well-formed (that is, an explicit conversion function such as explicit T::operator bool() const; is considered). Such expression e is said to be contextually converted to bool.
- the controlling expression of if, while, for;
- ...