0

Previous post indicated a rvalue reference can only bind to non-const rvalues: Difference between returning a const reference and rvalue reference

However, if that's true, how perfect forwarding works?

For this definition for perfect forwarding:

template <typename T, typename A1, typename A2>  
T* factory(A1&& a1, A2&& a2)  
{  
   return new T(std::forward<A1>(a1), std::forward<A2>(a2));  
}  

It seems the factory function could take any types of data: const lvalue, lvalue, rvalue.

(P.S. seems rvalue reference could do everything, but I rarely see code write by other using it, why not? It seems rvalue reference for a function argument could not only do the things a lvalue reference could do but also improves efficiency by avoiding unnecessary copies)

Community
  • 1
  • 1
lightrek
  • 951
  • 3
  • 14
  • 30
  • 1
    https://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers – imreal Mar 01 '17 at 18:59
  • 2
    Your arguments `A1` and `A2` are **not** rvalue references! Instead, they are *forwarding references*. These are something *entirely* different which just happens to use a similar looking syntax. – Dietmar Kühl Mar 01 '17 at 19:03

0 Answers0