2

I have some template class

template <typename T> class Base { };

I trying to inherit Derived class from Base and use Internal class that is nested of Derived as a generic argument of Base.

class Derived : Base<Internal> {
public: class Internal { };
}

But compiler doesn't see Internal in Base.

Is it possible to solve my problem with keeping Internal class as nested of Derived? And if it is possible - How?

Valentyn Zakharenko
  • 2,710
  • 1
  • 21
  • 47

1 Answers1

4

You can! Nope

(similar question)

I don't think that this can work. In order to define the class Derived you need to know the base class ("it must be a complete type") first; to instantiate the template Base into a class you need to have a type. Inner however is only a type "inside" of Derived; it's part of it's definition.


Definition of Derived  ---needs---> Base<Derived::Inner>
          ^                               |
          |----------needs----------------|

Even an indirection does not help:

template<typename T>
class Base {};

template<template<class> class X, typename T>
struct UseInner : public X<typename T::Inner> {};

class Derived : UseInner<Base, Derived>
{
    public: class Inner {};
};

In contrast to this, the CRTP works because there the template parameter can be an incomplete type ("no need to access internals").

Wrapping in a template doesn't work:

template<typename T>
class Base {};

template<template<class>class X>
struct Wrap
{
    struct Derived : X<typename Derived::Inner> {
        struct Inner {};
    };
};

using RealDerived = typename Wrap<Base>::Derived;
Daniel Jour
  • 15,896
  • 2
  • 36
  • 63