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

我的梦境 提交于 2020-01-14 08:00:10

问题


The base class in the CRTP pattern can access the member functions of the derived class, but it can't access a nested type in the derived class.

Why this difference?

To illustrate, consider the following piece of code:

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 causes a compilation error, while crtp_method compiles fine, although both attempt to access something defined in the Derived class. What is the C++ specification that explains that difference?


回答1:


The difference here is that instantiation of method happens only when you actually use it while instantiation of crtp_base happens at public crtp_base<X> where type X is still incomplete. The workaround would be to use type traits:

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;
};


来源:https://stackoverflow.com/questions/51553479/crtp-why-a-difference-between-getting-a-nested-type-and-nested-method-of-the-de

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!