In C++, it seems that a parameter may be normally expanded with ...
directly after the parameter pack's name. For example,
template <class... Tys>
void function(Tys... params) {
function(params...);
}
However, when using std::forward
, the ...
goes seemingly after the parameter pack's name. For example,
template <class... Tys>
void function(Tys... params) {
function(std::forward<Tys>(params)...); // The '...' is outside the parenthesis of 'params'
}
My question is why is there a difference? Why is the following code incorrect?
template<class... Tys>
void function(Tys... params) {
function(std::forward<Tys>(params...));
}
At what point in the parameter pack expanded? I probably am not fully understanding how std::forward
works or how it forwards arguments perfectly.
I appreciate any answers!
The ...
applies to the thing on its left.
function(params...)
expands to
function(param1, param2, ..., paramN)
.
function(std::forward<Tys>(params)...)
expands to
function(std::forward<Tys1>(param1), std::forward<Tys2>(param2), ..., std::forward<TysN>(paramN))
function(std::forward<Tys>(params...))
expands to
function(std::forward<Tys>(param1, param2, ..., paramN))