I always assumed that private inheritance simply means that a type doesn't tell the outside that it's inheriting from some base class. However, it seems that there are more restrictions.
Consider the following minimal example:
struct MyInterface {};
struct MyImpl : private MyInterface {};
struct Inherited : public MyImpl {
// Error: 'MyInterface' not accessible because 'MyImpl' uses 'private' to inherit from 'MyInterface'
void doSomething(MyInterface* mi) {}
};
struct Noninherited {
// All fine!
void doSomething(MyInterface* mi) {}
};
Clang, GCC, and MSVC all reject this code. Given my previous assumptions, I would have expected it to be fine.
doSomething
simply expects a pointer to MyInterface
, but it doesn't tell the outside world that Inherited
has MyInterface
in its inheritance hierarchy. To me, it seems that private inheritance doesn't only not tell the outside world about the inheritance structure but instead makes the whole inheritance structure completely "forget" that the inherited type even exists.
Is this the right "mental model" to understand private inheritance? Are there other unexpected restrictions to it?
This happens due to injected-class-name, unqualified name lookup rules and the fact the name lookup is completed before check for accessibility.
injected-class-name is a mechanism that makes class name available inside that class definition.
Now, the unqualified name lookup rules within a class definition state that first the scope of the class is searched, then the scopes of any base classes are searched recursively, and only after that (and some more steps) you perform normal search in namespace scope.
Putting this all together:
MyInterface
in scope of Inherited
- one as injected-class-name and one that resides in the same namespace as Inherited
(the global namespace).Inherited
first finds MyInterface
as injected-class-name inherited from MyImpl
. Name lookup is satisfied and doesn't search any longer for other instances of the name.MyInterface
inherited from MyImpl
is not accessible to Inherited
, because there is private
inheritance - an error happens.The way to fix that is to change unqualified name lookup into qualified one:
struct Inherited : public MyImpl {
void doSomething(::MyInterface* mi) {}
};
Now, injected-class-name cannot satisfy name lookup, because you explicitly ask for MyInterface
from global namespace, not any MyInterface
that happens to match. And since the name MyInterface
in global namespace is public (like all namespace names), it can used without any issue.