CRTP:为什么获得嵌套类型和派生类的嵌套方法有区别

CRTP: why a difference between getting a nested type and nested method of the derived class?

本文关键字:嵌套 方法 有区别 派生 为什么 嵌套类型 CRTP      更新时间:2023-10-16

CRTP模式中的基类可以访问派生类的成员函数,但不能访问派生类中的嵌套类型。

为什么会有这种差异?

为了进行说明,请考虑以下代码:

template<typename Derived>
struct crtp_base
{
void crtp_method() { return static_cast<Derived&>(*this).method(); } // compiles
using crtp_type = typename Derived::type; // doesn't compile
};
struct X : public crtp_base<X>
{
void method() {}
using type = int;
};
int main()
{
}

crtp_type导致编译错误,而crtp_method编译良好,尽管两者都试图访问Derived类中定义的内容。解释这种差异的C++规范是什么?

这里的区别在于,方法的实例化仅在实际使用时发生,而crtp_base的实例化发生在类型X仍然不完整的public crtp_base<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;
};