7

Is there a way in C++11/14/17 to access a member of the subclass in the parent class when using CRTP?

template <typename T>
class A {
public:
    using C = typename std::result_of<decltype(&T::next)(T)>::type;
};

class B : A<B> {
public:
    int next() { ... };
};

This should result in A<B>::C and B::C being int.

Niklas R
  • 16,299
  • 28
  • 108
  • 203

1 Answers1

9

No, I am afraid that is not possible. When A<B> is used as the base class, it must be instantiated (since a base class must be complete), but at that point B is still an incomplete type and thus its members cannot be accessed inside A<B>.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455