这里的区别在于,方法的实例化只在您在
crtp_base
发生在
public crtp_base<X>
where类型
X
仍然不完整。解决方法是使用类型特征:
template<typename x_Target>
struct Trait;
template<typename Derived>
struct crtp_base
{
void crtp_method() { return static_cast<Derived&>(*this).method(); }
using crtp_type = typename Trait<Derived>::type;
};
struct X;
template<>
struct Trait<X>
{
using type = int;
};
struct X : public crtp_base<X>
{
void method() {}
using type = Trait<X>::type;
};