Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving CRTP initialization order

I have some CRTP dependency that I am not sure how to resolve. Ideally I want to put as many things as possible in the base class, like functions, so I do not have to redefine those for every class that inherits those. This seems to cause an issue with the initialization order, where result_type is dependent on the type that is yet to be initialized. Here is an example: https://godbolt.org/z/YpfcPB

And here is the code:

template<typename T>
struct CRTP_Derived;

template<typename Derived>
struct CRTP
{
    using result_type = typename Derived::result_type;

};

template<typename T>
struct CRTP_Derived : public CRTP<CRTP_Derived<T>>
{
    using result_type = T;
};

int main()
{
    CRTP_Derived<int> a;
    return 0;
}
like image 339
lightxbulb Avatar asked Dec 11 '25 03:12

lightxbulb


1 Answers

I've also used a separate traits type for issues like this. You can reduce the needed boilerplate a little if you make the traits a second template parameter, instead of requiring users to specialize a separate template:

template<typename Derived, typename Traits>
struct CRTP
{
    using result_type = typename Traits::result_type;
};

template<typename T>
struct CRTP_Derived_Traits
{
    using result_type = T;
};

template<typename T>
struct CRTP_Derived : public CRTP<CRTP_Derived<T>, CRTP_Derived_Traits<T>>
{
};

int main()
{
    CRTP_Derived<int> a;
    return 0;
}
like image 105
aschepler Avatar answered Dec 13 '25 22:12

aschepler