繁体   English   中英

C ++模板化虚函数

[英]C++ Templated Virtual Function

C ++不支持模板化的虚拟成员函数,但我有一个理想的场景。 我想知道是否有人有想法如何实现这一目标。

#include <iostream>


class Foo {
public:
    virtual void bar(int ){}
    // make a clone of my existing data, but with a different policy
    virtual Foo* cloneforDB() = 0;
};


struct DiskStorage {
    static void store(int x) { std::cout << "DiskStorage:" << x << "\n"; }
};

struct DBStorage {
    static void store(int x) { std::cout << "DBStorage:" << x << "\n"; }
};

template<typename Storage>
class FooImpl : public Foo {
public:
    FooImpl():m_value(0) {}
    template<typename DiffStorage>
    FooImpl(const FooImpl<DiffStorage>& copyfrom) {
        m_value = copyfrom.m_value;
    }
    virtual void bar(int x) {
        Storage::store(m_value);
        std::cout << "FooImpl::bar new value:" << x << "\n";
        m_value = x;
    }
    virtual Foo* cloneforDB() {
        FooImpl<DBStorage> * newfoo = new FooImpl<DBStorage>(*this);
        return newfoo;
    }
    int m_value;
};

int main()
{
    Foo* foo1 = new FooImpl<DiskStorage>();
    foo1->bar(5);
    Foo* foo2 = foo1->cloneforDB();
    foo2->bar(21);
}

现在,如果我想克隆Foo implmemetation,但使用不同的Storagepolicy,我必须明确说明每个这样的实现:

cloneforDB()
cloneforDisk()

模板参数会简化它。 谁能想到更清洁的方法呢? 请关注这个想法而不是示例,因为它显然是一个人为的例子。

通常,如果要使用虚拟模板方法,则意味着类层次结构的设计出现问题。 其高级原因如下。

模板参数必须在编译时知道,这就是它们的语义。 它们用于保证代码的健全性。

虚函数用于多态,即。 在运行时动态调度。

因此,您不能将静态属性与运行时调度混合使用,如果您查看大图,则没有意义。

在这里,你在某个地方存储某些东西的事实不应该是你的方法类型的一部分,因为它只是一个行为特征,它可能在运行时发生变化。 所以这是错误的,包括在此方法的类型信息。

这就是为什么C ++不允许这样做:你必须依靠多态来实现这样的行为。

一个简单的方法是将指向Storage对象的指针作为参数传递(如果你只想为每个类提供一个对象,则为单例),并在虚函数中使用该指针。

这样,您的类型签名不依赖于您的方法的特定行为。 并且您可以在运行时更改存储(在此示例中)策略,这实际上是您应该要求的良好实践。

有时,行为可以由模板参数(例如Alexandrescu的策略模板参数)决定,但它是在类型级别而不是方法级别。

只需使用模板:

class Foo {
public:
    virtual void bar(int ){}

    template <class TargetType>
    Foo* clonefor() const;
};

class FooImpl { ... };

template 
inline <class TargetType>
Foo* Foo::clonefor() const
{
    return new FooImpl<TargetType>(*this);
}

现在叫它:

int main()
{
    Foo* foo1 = new FooImpl<DiskStorage>();
    foo1->bar(5);
    Foo* foo2 = foo1->clonefor<DBStorage>();
    foo2->bar(21);
}

我有时习惯解决这个问题的一个技巧是:

template<typename T>
using retval = std::vector<T const*>;
struct Bob {};

// template type interface in Base:
struct Base {
  template<typename T>
  retval<T> DoStuff();

  virtual ~Base() {};

// Virtual dispatch so children can implement it:
protected:
  virtual retval<int> DoIntStuff() = 0;
  virtual retval<double> DoDoubleStuff() = 0;
  virtual retval<char> DoCharStuff() = 0;
  virtual retval<Bob> DoBobStuff() = 0;
};

// forward template interface through the virtual dispatch functions:
template<> retval<int> Base::DoStuff<int>() { return DoIntStuff(); }
template<> retval<double> Base::DoStuff<double>() { return DoDoubleStuff(); }
template<> retval<char> Base::DoStuff<char>() { return DoCharStuff(); }
template<> retval<Bob> Base::DoStuff<Bob>() { return DoBobStuff(); }

// CRTP helper so the virtual functions are implemented in a template:
template<typename Child>
struct BaseHelper: public Base {
private:
  // In a real project, ensuring that Child is a child type of Base should be done
  // at compile time:
  Child* self() { return static_cast<Child*>(this); }
  Child const* self() const { return static_cast<Child const*>(this); }
public:
  virtual retval<int> DoIntStuff() override final { self()->DoStuff<int>(); }
  virtual retval<double> DoDoubleStuff() override final { self()->DoStuff<double>(); }
  virtual retval<char> DoCharStuff() override final { self()->DoStuff<char>(); }
  virtual retval<Bob> DoBobStuff() override final { self()->DoStuff<Bob>(); }
};

// Warning: if the T in BaseHelper<T> doesn't have a DoStuff, infinite
// recursion results.  Code and be written to catch this at compile time,
// and I would if this where a real project.
struct FinalBase: BaseHelper<FinalBase> {
  template<typename T>
  retval<T> DoStuff() {
    retval<T> ret;
    return ret;
  }
};

我从基于模板的调度,到虚拟函数调度,回到基于模板的调度。

接口是我想要分发的类型的模板。 一组有限的此类类型通过虚拟调度系统转发,然后在编译时重新分配到实现中的单个方法。

我承认这很烦人,并且能够说“我希望这个模板是虚拟的,但只能使用以下类型”会很好。

这有用的原因是它允许你编写与这些方法一致操作的类型无关的模板粘合代码,而不必像传递方法之类的指针那样做,或者写出提取哪种方法的类型特征包打电话。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM