If a using-declaration occurs at class scope, it is only allowed to redeclare a member of a base class. For example, using std::cout
is not allowed at class scope.
Often, the name redeclared is one that would otherwise be hidden. For example, in the below code, d1.foo
only refers to Derived1::foo(const char*)
and a compilation error will occur. The function Base::foo(int)
is hidden not considered at all. However, d2.foo(42)
is fine because the using-declaration brings Base::foo(int)
into the set of entities named foo
in Derived2
. Name lookup then finds both foo
s and overload resolution selects Base::foo
.
struct Base {
void foo(int);
};
struct Derived1 : Base {
void foo(const char*);
};
struct Derived2 : Base {
using Base::foo;
void foo(const char*);
};
int main() {
Derived1 d1;
d1.foo(42); // error
Derived2 d2;
d2.foo(42); // OK
}