开发者

Perfect forwarding - through virtual functions

开发者 https://www.devze.com 2023-02-01 23:49 出处:网络
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 te

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

精彩评论

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