mixing CRTP with SFINAE

喜你入骨 提交于 2019-12-03 21:46:17

This is a caveat of CRTP : at the time your base template is specialized for your non_default_impl class, i.e in its base classes list, non_default_impl itself is not defined yet.

Thus, any attempt to access anything that is part of its definition, for example the data_t typedef, fails.

Since you cannot use anything that is inside non_default_impl, a solution is to use an external type trait to choose your data_t :

template <class T>
struct dataType { typedef event_data<T> type; };

template <typename T>
struct base{
    typedef typename dataType<T>::type data_type;

    // ...
};

// Usage

struct non_default_data {};

template <>
struct dataType<struct non_default_impl> {
    typedef non_default_data type;
};

struct non_default_impl: public base<non_default_impl> {
    // ...
};

Note that you can't declare non_default_data inside non_default_impl, since it must be accessible from the type trait, which must be accessible from the CRTP, which still has to be specialized before non_default_impl is defined.

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