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
.
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>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With