문제

I need to represent a hierarchy like this:

template<typename T>
struct X
{
};

template<typename Derived = void>
struct Y : Y<void>
{
    //Note: not trying to use SFINAE here
    using DerivedType = typename std::enable_if<std::is_base_of<Y, Derived>::value, Derived>::type;
};
template<>
struct Y<void> : X<Y<void>>
{
};

struct Z : Y<Z>
{
};

Both Z and Y<void> need to be instantiable:

W<Y<>> wy;
W<Z> wz;

All Y<T> need to be instances of Y<void>, and if possible I would prefer not to have two different names to make it work. (That is my last resort)

The problem is, I don't know how to make this work. The above code obviously doesn't work as intended and doesn't compile. Is there a way I can make it work, or do you have any recommendations for alternatives besides the one I already mentioned?

도움이 되었습니까?

해결책

How about reordering it:

template <typename> struct Y;

template <> struct Y<void> : X<Y<void>> { };

template <typename T = void> struct Y : Y<void> { };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top