0

我需要表示这样的层次结构:

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

Z 和 Y<void> 都需要可实例化:

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

所有 Y<T> 都需要是 Y<void> 的实例,如果可能的话,我不希望有两个不同的名称来使其工作。(这是我最后的手段)

问题是,我不知道如何使这项工作。上面的代码显然不能按预期工作并且不能编译。有没有办法让它工作,或者除了我已经提到的那个之外,你有什么替代方案的建议吗?

4

1 回答 1

3

如何重新排序:

template <typename> struct Y;

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

template <typename T = void> struct Y : Y<void> { };
于 2013-05-03T16:44:39.567 回答