【问题标题】:Accessing template member functions of template class through pointer to virtual base class?通过指向虚拟基类的指针访问模板类的模板成员函数?
【发布时间】:2014-05-01 23:26:01
【问题描述】:

我正在尝试实现将通过非模板虚拟基类指针访问的一系列模板化对象。简化后,基类如下所示:

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;
}

Mac OS 10.8.5 上的 clang++ 给出了以下反馈:

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&lt;double&gt;::printThing(const int &amp;),编译器声称它不存在。并且通用的 Derived&lt;T&gt;::printThing(const U &amp;) 成员函数似乎没有被实例化。

是否有任何可移植的方式告诉编译器我打算为每个“未实现”的虚拟方法实例化通用模板成员函数?

我尝试了很多替代方案,但目前唯一可行的是为基类成员函数提供默认实现,并编写 Derived 的包装器,为所需的 U 类型显式实现 printThing()。

【问题讨论】:

  • 函数模板不会覆盖虚函数。 [temp.mem]“3 成员函数模板不应是虚拟的。4 成员函数模板的特化不会覆盖基类中的虚拟函数。”便携故事结束。
  • 谢谢 dyp。这个理论就这么多。我愿意接受有关如何做到这一点的建议,而无需在类层次结构中的任何地方编写样板代码或包装类。

标签: c++ templates pointers polymorphism template-specialization


【解决方案1】:

我使用奇怪重复模板模式 (CRTP) explained here by Eli Bendersky 找到了答案。

需要再增加一层模板类:

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);
};

通过这个添加,以及对 Derived 的简单更改以从 Adapter 继承,程序安抚了编译器,而且更好的是,生成了我正在寻找的结果:

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$ 

【讨论】:

  • 我刚刚确认Derived&lt;T&gt; 本身可以是一个虚拟基类,将printThingInternal(const T &amp;thing) 的实现委托给派生自它的类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-13
  • 2012-07-05
  • 1970-01-01
  • 2011-12-09
  • 2015-12-17
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多