Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfect forwarding - through virtual functions

Tags:

c++11

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

like image 787
Puppy Avatar asked Jan 03 '11 13:01

Puppy


1 Answers

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;
};
like image 185
ltjax Avatar answered Oct 13 '22 19:10

ltjax