I have a class Base
defining an explicit operator bool
:
struct Base {
virtual explicit operator bool() const {
return true;
}
};
And I have a subclass Derived
, defining an operator bool
:
struct Derived : Base {
operator bool() const override {
return false;
}
};
As you can observe, Derived::operator bool
is explicitly not marked explicit
, but marked override
, so I expected the compiler to complain. However, both gcc and clang seem to agree that this is valid. Was my expectation unreasonable?
Moreover, if I use the classes as follows, TakesBool(base)
does not compile (as expected), but TakesBool(derived)
does:
void TakesBool(bool b) {}
int main() {
//Base base; TakesBool(base); // compilation error (as expected)
Derived derived; TakesBool(derived);
return 0;
}
This seems to indicate that Derived
has an (non-explicit
) operator bool
, which, however, is marked override
without a virtual
declaration. How is this possible?