Perfect call forwarding through virtual functions

How to enable perfect forwarding through a virtual function? I really have no desire to write every overload, as in C ++ 03.

+5
source share
1 answer

You can not. Perfect forwarding only works by combining templates and rvalue links, because it depends on what type of real type T&&is evaluated when T is specialized. You cannot mix templates and virtual functions.

However, you can solve your problem with a kind of type-erasing 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;
};
+3
source

All Articles