开发者

Perfect forwarding - through virtual functions

How can I e开发者_Python百科nable perfect forwarding through a virtual function? I really have no desire to write every overload like in C++03.


You can't. Perfect forwarding only works by combining templates and rvalue-references, because it depends on what kind of real type T&& evaluates to when T is specialized. You cannot mix templates and virtual functions.

However, you can might be able to solve your problem by some kind of type-erasure mechanism:

struct base {
  virtual void invoke() = 0;
};

template <class T>
struct derived : public base {
  derived( T&& yourval ) : m_value(std::forward(yourval)) {}
  virtual void invoke() { /* operate on m_value.. */ }

  T&& m_value;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜