Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access a member inside the parent class body with CRTP? [duplicate]

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.

like image 214
Niklas R Avatar asked Apr 27 '18 15:04

Niklas R


1 Answers

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>.

like image 75
Angew is no longer proud of SO Avatar answered Nov 10 '22 23:11

Angew is no longer proud of SO