why pass by universal reference?

I understand by universal reference the argument passing following this pattern:

1
2
template<typename T>
void f(T&& t)


as in

1
2
3
template< class ExecutionPolicy, class RandomIt >
void sort( ExecutionPolicy&& policy,
           RandomIt first, RandomIt last );


Why do we need to do this? why not pass by "normal" reference?

Universal references are useful when you want to move the argument if it is an rvalue and copy it if it is a "normal" lvalue reference. To accomplish this, std::forward is often used. To do the same without forwarding reference would require two separate overloads.
Last edited on
Topic archived. No new replies allowed.