我有一个模板函数,它是为类显式实例化的Base,但不是为Derived类实例化的。如何强制传递Derived类(或其他派生类)的用途与该类匹配Base?
头文件:
class Base {
};
class Derived : public Base {
};
class Derived2 : public Base {
};
template <typename Example> void function(Example &arg);
实现文件:
// Explicitly instantiate Base class:
template void function<Base>(Base &arg);
// Define the template function:
template <typename Example> void function(Example &arg) {
// Do something.
}
因为我没有为Derivedor显式实例化函数Derived2,所以我得到未定义的引用,但是,我想绑定Base显式定义的类。
对于使用 C++-03 从 Base 派生的所有对象,如何强制模板解析为 Base 类?
Derived我可以通过将类专业化到Base类定义以某种方式做到这一点吗?