Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:21:01+00:00 2026-05-25T03:21:01+00:00

C++0x shows an example of using std::forward : template<class T> void foo(T&& arg) {

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:21:02+00:00Added an answer on May 25, 2026 at 3:21 am

    Use it like your first example:

    template <typename T> void f(T && x)
    {
      g(std::forward<T>(x));
    }
    
    template <typename ...Args> void f(Args && ...args)
    {
      g(std::forward<Args>(args)...);
    }
    

    That’s because of the reference collapsing rules: If T = U&, then T&& = U&, but if T = U&&, then T&& = U&&, so you always end up with the correct type inside the function body. Finally, you need forward to turn the lvalue-turned x (because it has a name now!) back into an rvalue reference if it was one initially.

    You should not forward something more than once however, because that usually does not make sense: Forwarding means that you’re potentially moving the argument all the way through to the final caller, and once it’s moved it’s gone, so you cannot then use it again (in the way you probably meant to).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
Is there any way to write Shoes application using IDE (NetBeans for example)? I
In Linq Dynamic Query , Scott Guthrie shows an example Linq query: var query
i am looking for a basic example that shows how i could call a
I'm looking for a good example that shows how to handle (in the controller)
David Flanagan's excellent book on JavaScript has an example that shows how to perform
The example on this page only shows Groovy assertions without parentheses. assert a !=
Whew, that was a long title. Here's my problem. I've got a template class
Note: Full working example now below. Original question follows: I'm having problems using ld's
The contrived example below shows the duplicate addresses seen inside of a loop. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.