CRTP:为什么获取嵌套类型和派生类的嵌套方法之间存在差异?

Jon*_*ara 7 c++ templates crtp

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()
{

}
Run Code Online (Sandbox Code Playgroud)

crtp_type导致编译错误,虽然crtp_method编译都很好,但两者都试图访问Derived类中定义的内容.什么是解释这种差异的C++规范?

use*_*670 8

这里的区别在于,方法的实例化只在您实际使用它时crtp_base发生,而实例化public crtp_base<X>在类型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;
};
Run Code Online (Sandbox Code Playgroud)