是否可以使用 CRTP 访问父 class 体内的成员?
Is it possible to access a member inside the parent class body with CRTP?
在 C++11/14/17 中有没有办法在使用 CRTP 时访问父 class 中子 class 的成员?
template <typename T>
class A {
public:
using C = typename std::result_of<decltype(&T::next)(T)>::type;
};
class B : A<B> {
public:
int next() { ... };
};
这应该导致 A<B>::C
和 B::C
为 int
。
不,恐怕这是不可能的。当 A<B>
用作基础 class 时,它必须被实例化(因为基础 class 必须是完整的),但此时 B
仍然是一个不完整的类型并且因此无法在 A<B>
.
中访问其成员
在 C++11/14/17 中有没有办法在使用 CRTP 时访问父 class 中子 class 的成员?
template <typename T>
class A {
public:
using C = typename std::result_of<decltype(&T::next)(T)>::type;
};
class B : A<B> {
public:
int next() { ... };
};
这应该导致 A<B>::C
和 B::C
为 int
。
不,恐怕这是不可能的。当 A<B>
用作基础 class 时,它必须被实例化(因为基础 class 必须是完整的),但此时 B
仍然是一个不完整的类型并且因此无法在 A<B>
.