KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
C++0x shows an example of using std::forward : template<class T> void foo(T&& arg) { bar(std::forward<T>(arg)); } When is it advantageous to use std::forward , always? Also, it requires to use && in the parameters declaration, is it valid in all cases? I thought you had to pass temporaries to a function if the function was declared with && in it, so can foo be called with any parameter? Lastly, if I have a function call such as this: template<int val, typename... Params> void doSomething(Params... args) { doSomethingElse<val, Params...>(args...); } Should I use this instead: template<int val, typename... Params> void doSomething(Params&&... args) { doSomethingElse<val, Params...>(std::forward<Params>(args)...); } Also, if use the parameters twice in the function, i.e. forwarding to two functions at the same time, is it wise to use std::forward ? Won't std::forward convert the same thing to a temporary twice, moving the memory and make it invalid for a second use? Would the following code be ok: template<int val, typename... Params> void doSomething(Params&&... args) { doSomethingElse<val, Params...>(std::forward<Params>(args)...); doSomethingWeird<val, Params...>(std::forward<Params>(args)...); } I'm a bit confused by std::forward , and I'd gladly use some clearing up.
Tags (comma-separated)
Save Edits
Cancel