An rvalue reference can only bind to non-const rvalues?

55 Views Asked by At

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)

0

There are 0 best solutions below