Typedef 在 CRTP 中创建(假)派生 class - 不可能?

Typedef to create a (fake) derived class in CRTP - impossible?

如何在 CRTP 中为类似于派生 class 的名称添加别名?

难道(Derived1)根本不可能,我必须完全创建一个新的class(Derived2)?

template<class CRTP,class T>class Base{};

//These don't work.
//template<class T> using Derive1 = Base<Derive1<T>,T>;
//template<class T> using Derive1 = Base<Base,T>;
//template<class T> using Derive1 = Base<Base<T>,T>;
//template<class T> using Derive1 = Base<Base<Base<T>>,T>;

//This works.
template<class T> class Derive2 : public Base<Derive2<T>,T>{};
int main(){
    //Derive1<int> d1;
    Derive2<int> d2;
}

Demo

关于typedefCRTP的问题有很多,但我找不到符合我的问题。

尝试递归使用 Base 的别名将不起作用,因为 Base 的每次使用都需要您提供 2 个模板参数,但您永远不会这样做,因此排除了这些别名:

template<class T> using Derive1 = Base<Base,T>;
template<class T> using Derive1 = Base<Base<T>,T>;
template<class T> using Derive1 = Base<Base<Base<T>>,T>;

请注意,如果您接受别名的另一个模板参数,则第 3rd 个将会成功:

template<class T, class U> using Derive1 = Base<Base<T, U>,T>;

这给我们留下了这个:

template<class T> using Derive1 = Base<Derive1<T>,T>;

这个不起作用的原因是,在 = 运算符的右侧,还没有名为 Derive1 的类型存在,而在左侧,您使用与此类类型相同的名称。那么你最终会遇到与以前相同的问题(递归引用),只是这是不可能的,因为不仅名为 Derive1 的类型不存在用于模板参数中,而且即使它做了编译器会抱怨你试图重用相同的符号。

我怀疑你问的是 X-Y Problem,所以如果你提供关于 如何 你想使用这些别名的额外上下文,我可以为你提供一个替代解决方案.

这与CRTP无关。

using 创建一个 别名 ;您正在尝试创建一个完全 newseparate 类型。

不清楚为什么您希望它起作用,或者为什么显示的解决方案不充分。

所以,是的,这是不可能的。