通过指向虚拟基类的指针访问模板类的模板成员函数
Accessing template member functions of template class through pointer to virtual base class?
我正在尝试实现一系列模板化对象,这些对象将通过非模板虚拟基类指针进行访问。简化后,基类看起来像:
class Base
{
public:
virtual void printThing(const int &thing) = 0;
virtual void printThing(const double &thing) = 0;
virtual void printThing(const bool &thing) = 0;
};
我想做的事情在下面的派生类实现中进行了概述:
#include <iostream>
template <typename T>
class Derived : public Base
{
public:
void printThing(const T &thing);
template <typename U>
void printThing(const U &thing);
};
template <typename T>
void Derived<T>::printThing(const T &thing)
{
std::cout << "Derived same type " << thing << std::endl;
}
template <typename T>
template <typename U>
void Derived<T>::printThing(const U &thing)
{
std::cout << "Derived diff type " << thing << std::endl;
}
template <>
template <>
void Derived<double>::printThing(const int &thing)
{
std::cout << "Derived<double> specialized for int " << thing << std::endl;
}
这对任何类型的U都有效——只要代码直接在Derived的实例上调用成员函数,并且U在编译时是已知的。
但是,当我试图通过指向Base的指针访问Derived时,我会遇到编译器错误,如下面的测试程序所示:
int main(int argc, char* argv[])
{
Derived<int> dint;
Derived<double> ddouble;
Base * bint = &dint;
Base * bdouble = &ddouble;
double d = 3.14;
int i = 42;
bint->printThing(i);
bint->printThing(d);
bdouble->printThing(i);
bdouble->printThing(d);
return 0;
}
clang++在Mac OS 10.8.5上给出了这样的反馈:
razor:playpen cfry$ clang++ template-specialization.cc
template-specialization.cc:43:16: error: variable type 'Derived<int>' is an abstract class
Derived<int> dint;
^
template-specialization.cc:7:16: note: unimplemented pure virtual method 'printThing' in 'Derived'
virtual void printThing(const double &thing) = 0;
^
template-specialization.cc:8:16: note: unimplemented pure virtual method 'printThing' in 'Derived'
virtual void printThing(const bool &thing) = 0;
^
template-specialization.cc:44:19: error: variable type 'Derived<double>' is an abstract class
Derived<double> ddouble;
^
template-specialization.cc:6:16: note: unimplemented pure virtual method 'printThing' in 'Derived'
virtual void printThing(const int &thing) = 0;
^
template-specialization.cc:8:16: note: unimplemented pure virtual method 'printThing' in 'Derived'
virtual void printThing(const bool &thing) = 0;
^
2 errors generated.
razor:playpen cfry$
请注意,我已经显式地实现了Derived<double>::printThing(const int &)
,编译器声称它不存在并且广义的Derived<T>::printThing(const U &)
成员函数似乎没有被实例化。
有没有任何可移植的方法可以告诉编译器,我打算为每个"未实现"的虚拟方法实例化通用模板成员函数?
我已经尝试了很多替代方案,但到目前为止,唯一有效的方案是为基类成员函数提供一个默认实现,并编写Derived的包装器,该包装器为所需的U类型显式实现printThing((。
我使用Eli Bendersky在这里解释的Curioly Recurring Template Pattern(CRTP(找到了答案。
它需要添加另一层模板类:
template <class Child>
class Adapter : public Base
{
public:
void printThing(const int &thing)
{
static_cast<Child *>(this)->printThingInternal(thing);
}
void printThing(const double &thing)
{
static_cast<Child *>(this)->printThingInternal(thing);
}
void printThing(const bool &thing)
{
static_cast<Child *>(this)->printThingInternal(thing);
}
};
template <typename T>
class Derived : public Adapter<Derived <T> >
{
public:
void printThingInternal(const T &thing);
template <typename U>
void printThingInternal(const U &thing);
};
有了这个添加,以及从Adapter继承Derived的简单更改,该程序安抚了编译器,更好的是,生成了我想要的结果:
razor:playpen cfry$ clang++ template-specialization.cc
razor:playpen cfry$ ./a.out
Derived same type 42
Derived diff type 3.14
Derived<double> specialized for int 42
Derived same type 3.14
razor:playpen cfry$
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法