伪造虚拟模板函数c ++

时间:2012-05-13 21:39:36

标签: c++ templates generics boost

我意识到c ++中不允许使用虚拟模板函数。由于我的特定应用程序域,我们处理多组算法(通过多态和继承实现自然)并需要强制执行通用接口。特定的算法类在迭代器上工作(这并不奇怪),但是我们想通过这些模板化函数伪造虚拟化。以下是我们使用boost :: mpl提出的解决方案示例。我意识到这很冗长,但这是一个最小的代码示例,我可以创建它来模拟我的目标。我的具体问题在代码之后。

#include <iostream>
#include <vector>
#include <boost/mpl/list.hpp>
#include <boost/mpl/for_each.hpp>

using namespace std;

class A;
class B;
class C;

typedef boost::mpl::list<B, C> DerivedClassList; 

template<typename Base, typename Iterator>
struct VirtualFunc{
  public:
    VirtualFunc(Base* _memory, Iterator _begin, Iterator _end) : 
      m_memory(_memory), m_begin(_begin), m_end(_end){}

    template<typename T>
      void operator()(T& _t) {
        T* tptr = dynamic_cast<T*>(m_memory);
        if(tptr != NULL){
          tptr->Print(m_begin, m_end);
        }   
      }   

  private:
    Base* m_memory;
    Iterator m_begin, m_end;
};  

class A{
  public:
    A(){}
    virtual ~A(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        boost::mpl::for_each<DerivedClassList>(VirtualFunc<A, Iterator>(this, _begin, _end));
      }   
};  

class B : public A {
  public:
    B(){}
    virtual ~B(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        cout << "Begin::" << *_begin << endl;
      }
};

class C : public A {
  public:
    C(){}
    virtual ~C(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        for(Iterator it = _begin; it!=_end; it++)
          cout << "Iterator::" << *it << endl;
      }
};

int main(){
  vector<size_t> numbers;
  for(size_t i = 0; i<5; i++)
    numbers.push_back(i);

  A* printBegin = new B();
  A* printAll = new C();
  //faking virtualism will print just begin
  printBegin->Print(numbers.begin(), numbers.end());
  //faking virtualism will print all
  printAll->Print(numbers.begin(), numbers.end());
}

那么这个“假虚拟”模板函数的缺陷是什么?有没有更简洁的方法来做到这一点?

也是代码标准的借口,它们就是我们在工作场所使用的。

1 个答案:

答案 0 :(得分:0)

为什么不用经典的双重调度模式替换。您似乎知道基础级别的类层次结构 - 所以我将使用以下内容。众所周知,Visitor或DoubleDispatch模式并消除了非高效的dynamic_cast。坦率地说 - 如果我看到dynamic_cast&lt;&gt;我总是考虑双重调度,

已知的clasees:

class A;
class B;
class C;

虚拟主义的起点:

class IVirtualFunc {
  public:
    virtual void callFor(B& memory) = 0;
    virtual void callFor(C& memory) = 0;
};  

模板参数的实现:

template<typename Iterator>
class VirtualFunc : public IVirtualFunc {
  public:
    VirtualFunc (Iterator _begin, Iterator _end) : begin(_begin), end(_end) {}
    virtual void callFor(B& memory);
    virtual void callFor(C& memory);
  private:
    Iterator begin;
    Iterator end;   
};

实际实现的抽象基类:

class A{
  public:
    template<typename Iterator>
    void Print(Iterator _begin, Iterator _end) {
        VirtualFunc<Iterator> vFunc(_begin, _end);
        dispatch(vFunc);   
    }
    virtual void dispatch(IVirtualFunc&) = 0;   
};  

首次使用双重调度实际实现(VirtualFunc<Iterator>::callFor(B& b)):

class B : public A {
  public:
    B(){}
    virtual ~B(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        cout << "Begin::" << *_begin << endl;
      }
    virtual void dispatch(IVirtualFunc& vf) { vf.callFor(*this); }   
};

template<typename Iterator>
void VirtualFunc<Iterator>::callFor(B& b)
{
     b.Print(begin, end);
}

对其进行双重调度的第二个实际实现(VirtualFunc<Iterator>::callFor(C& c)):

class C : public A {
  public:
    C(){}
    virtual ~C(){}

    template<typename Iterator>
      void Print(Iterator _begin, Iterator _end){
        for(Iterator it = _begin; it!=_end; it++)
          cout << "Iterator::" << *it << endl;
      }
    virtual void dispatch(IVirtualFunc& vf) { vf.callFor(*this); }   
};
template<typename Iterator>
void VirtualFunc<Iterator>::callFor(C& c)
{
     c.Print(begin, end);
}

它证明了它的作用:

int main(){
  vector<size_t> numbers;
  for(size_t i = 0; i<5; i++)
    numbers.push_back(i);

  A* printBegin = new B();
  A* printAll = new C();
  //faking virtualism will print just begin
  printBegin->Print(numbers.begin(), numbers.end());
  //faking virtualism will print all
  printAll->Print(numbers.begin(), numbers.end());
}

输出:

Begin::0
Iterator::0
Iterator::1
Iterator::2
Iterator::3
Iterator::4