why pass by universal reference?

Aug 17, 2024 at 8:45pm
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?

Aug 18, 2024 at 8:23am
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 Aug 18, 2024 at 8:23am
Topic archived. No new replies allowed.