你可以用当前的 C++ 来完成。您为模板提供了“足够多”的参数,并为它们提供了默认值:
class nothing1 {};
class nothing2 {};
class nothing3 {};
template <class T1 = nothing1, class T2 = nothing2, class T3 = nothing3>
class X : public T1, public T2, public T3 {};
或者您可以变得更复杂并使用递归。首先,前向声明模板:
class nothing {};
template <class T1 = nothing, class T2 = nothing, class T3 = nothing>
class X;
然后你专门针对所有参数都是默认的情况:
template <>
class X<nothing, nothing, nothing> {};
然后,您正确定义通用模板(之前您仅向前声明):
template <class T1, class T2, class T3>
class X : public T1, public X<T2, T3>
请注意,在基类中,您继承了 X 但缺少第一个参数。所以它们都沿着一个地方滑动。最终它们都将成为默认值,并且专业化将启动,它不会继承任何内容,从而终止递归。
Update:只是有一种奇怪的感觉 我以前发过类似的东西你猜怎么着... https://stackoverflow.com/questions/672843/can-templates-be-used-to-access-struct-variables-by-name/672913#672913