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;
};
精彩评论