When to choose Args&& instead of Args?

Hi,

I have a simple question. When do we choose this implementation:

1
2
3
4
template<class... Args>
internal::multi_order_by_t<Args...> multi_order_by(Args&&... args) {
    return {std::make_tuple(std::forward<Args>(args)...)};
}


instead of

1
2
3
4
template<class... Args>
internal::multi_order_by_t<Args...> multi_order_by(Args... args) {
    return {std::make_tuple(std::forward<Args>(args)...)};
}


in other words, when do we choose Args&& over Args& or Args ?

Regards,
Juan
Rules of thumb for function parameters are:
Forwarding references should be passed to forward;
Rvalue references should be passed to move.

See the Core Guidelines for more info, in particular F.15 through F.19:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-functions
Last edited on
You use std::forward with && for perfect forwarding. As the type of Args is unknown, don't pass by value as a copy constructor may not be available or if it is it may be operationally 'expensive'. When having an arg that is passed as an arg to another then rule of thumb is to have the arg as && and use std::forward.
Topic archived. No new replies allowed.