How would one call std::forward on all arguments in a variadic function?

C++C++11Variadic TemplatesRvalue ReferencePerfect Forwarding

C++ Problem Overview


I was just writing a generic object factory and using the boost preprocessor meta-library to make a variadic template (using 2010 and it doesn't support them). My function uses rval references and std::forward to do perfect forwarding and it got me thinking...when C++0X comes out and I had a standard compiler I would do this with real variadic templates. How though, would I call std::forward on the arguments?

template <typename ...Params>
void f(Params... params) // how do I say these are rvalue reference?
{
    y(std::forward(...params)); //? - I doubt this would work.
}

Only way I can think of would require manual unpacking of ...params and I'm not quite there yet either. Is there a quicker syntax that would work?

C++ Solutions


Solution 1 - C++

You would do:

template <typename ...Params>
void f(Params&&... params)
{
    y(std::forward<Params>(params)...);
}

The ... pretty much says "take what's on the left, and for each template parameter, unpack it accordingly."

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward StrangeView Question on Stackoverflow
Solution 1 - C++GManNickGView Answer on Stackoverflow