Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 530
Jonathan Boccara Avatar asked Jul 27 '18 08:07

Jonathan Boccara


1 Answers

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;
};
like image 82
user7860670 Avatar answered Sep 23 '22 06:09

user7860670