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?
You can use a conversion operator when there is a natural and clear conversion to or from a different type. In general, any unit of measure is a good candidate for this.
Conversion Operators in C++ C++ supports object oriented design. So we can create classes of some real world objects as concrete types. Sometimes we need to convert some concrete type objects to some other type objects or some primitive datatypes. To make this conversion we can use conversion operator.
You might think that the non-explicit operator bool in Derived doesn't override explicit operator bool in Base, no, it does. The explicit specifier doesn't matter, it's not a part of function signature.
From the standard, §10.3/2 Virtual functions [class.virtual]:
(emphasis mine)
If a virtual member function
vfis declared in a classBaseand in a classDerived, derived directly or indirectly fromBase, a member functionvfwith the same name, parameter-type-list ([dcl.fct]), cv-qualification, and ref-qualifier (or absence of same) asBase::vfis declared, thenDerived::vfis also virtual (whether or not it is so declared) and it overridesBase::vf.
So the compiler will complain only when the name, parameter-type-list, cv-qualification or ref-qualifier of the function doesn't match, the explicit specifier won't be considered.
You said "marked override without a virtual declaration", note that the virtual declaration for member function in derived class is superfluous, it's also virtual.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With