有没有办法从其专业化中调用通用 class 模板的实现?
Is there a way to invoke the implementation of the generic class template from its specialization?
我有模板class
template<typename T>
class D : public B {...}
其中 B 是在 class D 中实现的纯虚拟接口。但是,对于一种类型 T0,该实现具有附加数据和处理该数据的附加成员函数。因此,我必须专门针对这种情况 class D:
template<>
class D<T0> : public B {...}
同时,本特化中所有其他方法的实现与泛型完全相同,我不想复制粘贴。问题是,如果可能的话,如何从 class D<T0>
中调用 class D 中的通用实现。
PS。当然,正如@MaxLanghof 和@Klaus 所建议的那样,有一个中间派生 class C 的解决方案。但是,在此解决方案中,可以实例化我不想要的 class C<T0>
。如果没有其他解决方案,我将使用此解决方案。也许在 C++11/14 中有一些现代结构可以在这种情况下提供帮助?
At the same time, the implementation of all other methods in this specialization is exectly the same as in the generic case.
因此,您应该将通用代码拆分为一个公共基础 class,并从新的中间体 class 派生出通用模板和专用模板。在模板专业化上进行完整的代码重复是没有意义的。
由于这个通用代码现在是一个通用基础 class,您可以简单地调用这些方法,而无需任何特殊语法或技巧,因为它们很常见。
编辑:关于评论:How to protect to instantiate a object of intermediate class:
您在 C++ 中有关键字 protected
,这使得保护对构造函数的访问成为可能。
示例:
class B{};
class T0;
template<typename T>
class C : public B /* all the non-specialized implementation of D */
{
protected: C() {}
};
// Use the non-specialized implementation by default
template<typename T>
class D : public C<T> {};
// Add more functionality in the specialized case
template<>
class D<T0> : public C<T0> { /* your additional functionality */ };
int main()
{
D<int> dint;
D<T0> dt0;
C<int> Cint; // fails
C<T0> Ct0; // fails
}
再添加一层。
template<typename T>
class C : public B { /* all the non-specialized implementation of D */ };
// (possibly hide this in some implementation detail namespace)
// Use the non-specialized implementation by default
template<typename T>
class D : public C {};
// Add more functionality in the specialized case
template<>
class D<T0> : public C { /* your additional functionality */ };
我有模板class
template<typename T>
class D : public B {...}
其中 B 是在 class D 中实现的纯虚拟接口。但是,对于一种类型 T0,该实现具有附加数据和处理该数据的附加成员函数。因此,我必须专门针对这种情况 class D:
template<>
class D<T0> : public B {...}
同时,本特化中所有其他方法的实现与泛型完全相同,我不想复制粘贴。问题是,如果可能的话,如何从 class D<T0>
中调用 class D 中的通用实现。
PS。当然,正如@MaxLanghof 和@Klaus 所建议的那样,有一个中间派生 class C 的解决方案。但是,在此解决方案中,可以实例化我不想要的 class C<T0>
。如果没有其他解决方案,我将使用此解决方案。也许在 C++11/14 中有一些现代结构可以在这种情况下提供帮助?
At the same time, the implementation of all other methods in this specialization is exectly the same as in the generic case.
因此,您应该将通用代码拆分为一个公共基础 class,并从新的中间体 class 派生出通用模板和专用模板。在模板专业化上进行完整的代码重复是没有意义的。
由于这个通用代码现在是一个通用基础 class,您可以简单地调用这些方法,而无需任何特殊语法或技巧,因为它们很常见。
编辑:关于评论:How to protect to instantiate a object of intermediate class:
您在 C++ 中有关键字 protected
,这使得保护对构造函数的访问成为可能。
示例:
class B{};
class T0;
template<typename T>
class C : public B /* all the non-specialized implementation of D */
{
protected: C() {}
};
// Use the non-specialized implementation by default
template<typename T>
class D : public C<T> {};
// Add more functionality in the specialized case
template<>
class D<T0> : public C<T0> { /* your additional functionality */ };
int main()
{
D<int> dint;
D<T0> dt0;
C<int> Cint; // fails
C<T0> Ct0; // fails
}
再添加一层。
template<typename T>
class C : public B { /* all the non-specialized implementation of D */ };
// (possibly hide this in some implementation detail namespace)
// Use the non-specialized implementation by default
template<typename T>
class D : public C {};
// Add more functionality in the specialized case
template<>
class D<T0> : public C { /* your additional functionality */ };