We have the following simple (and slightly modified to add main
and output) example in the Standard:
struct A {
virtual void f()
{
cout << "A\n";
}
};
struct B : virtual A {
virtual void f()
{
cout << "B\n";
}
};
struct C : B, virtual A {
using A::f;
};
int main()
{
C c;
c.f(); // calls B::f, the final overrider
c.C::f();
return 0;
}
From which we can make a conclusion that using A::f
does not present an overrider. But what wording in the Standard dictates it? Here is the wording for the final overrider from the C++17 draft ([class.virtual]p2):
<...> A virtual member function C::vf of a class object S is a final overrider unless the most derived class (4.5) of which S is a base class subobject (if any) declares or inherits another member function that overrides vf. In a derived class, if a virtual member function of a base class subobject has more than one final overrider the program is ill-formed.
And I wasn't able to find what "overrides" actually mean. If it is not defined and we consider any declaration as an overrider then we should consider the using declaration to be an overrider since [namespace.udecl]p2 says:
Every using-declaration is a declaration and a member-declaration and can therefore be used in a class definition.
I understand the intent of the Standard for using declaration to not introduce an overrider, but can someone point me to the actual quotes which say that in Standardese? That is the first part, now to the second one
Consider the following code:
#include <iostream>
#include <string>
using std::cout;
class A {
public:
virtual void print() const {
cout << "from A" << std::endl;
}
};
class B: public A {
public:
void print() const override {
cout << "from B" << std::endl;
}
};
class C: public A {
public:
void print() const override {
cout << "from C" << std::endl;
}
};
class D: public B, public C {
public:
using C::print;
};
int main()
{
D d{};
d.print();
return 0;
}
If the using declaration doesn't introduce an overrider then we have 2 final overriders in D
, hence—an undefined behavior because of
In a derived class, if a virtual member function of a base class subobject has more than one final overrider the program is ill-formed.
Right?