I would like to inherit from a class template, but pass a member class of the inheriting class as a template parameter to the base. Is this even possible? Here's a minimal example.
template<typename T>
class Base { /* stuff involving T */ };
template <typename U>
class Derived : Base<typename Derived<U>::Member> {
public:
class Member { /* stuff involving U */};
/* stuff involving Member, Base<Member> and U */
};
This compiles fine until I try to create an instance of Derived
(eg. Derived<int>
) at which point g++ (7.2.0) tells me that I'm using an incomplete type.
In instantiation of ‘class Derived<int>’:
error: invalid use of incomplete type ‘class Derived<int>’
class Derived : Base<typename Derived<U>::Member> {
^~~~~~~
I can kind of see the problem: the instantiation of Base
requires knowledge of Derived::Member
, which hasn't yet been defined, but is there any way around this?
As an alternative, I could have Member
outside Derived
, but then I lose the advantages of having it as a member class (access to private and protected members of Derived
, encapsulation of Member
, etc...). I also considered making Member
a member of Base
instead, but logically, the contents of Member
are specific to Derived
and don't belong in the more general Base
.