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 6915229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:26:58+00:00 2026-05-27T09:26:58+00:00

Why is the following code valid: template<typename T1> void foo(T1 &&arg) { bar(std::forward<T1>(arg)); }

  • 0

Why is the following code valid:

template<typename T1>
void foo(T1 &&arg) { bar(std::forward<T1>(arg)); }

std::string str = "Hello World";
foo(str); // Valid even though str is an lvalue
foo(std::string("Hello World")); // Valid because literal is rvalue

But not:

void foo(std::string &&arg) { bar(std::forward<std::string>(arg)); }

std::string str = "Hello World";
foo(str); // Invalid, str is not convertible to an rvalue
foo(std::string("Hello World")); // Valid

Why doesn’t the lvalue in example 2 get resolved in the same manner that it does in example 1?

Also, why does the standard feel it important to need to provide the argument type in std::forward<T> versus simple deducing it? Simply calling forward is showing intention, regardless of the type.

If this isn’t a standard thing and just my compiler, I am using msvc10, which would explain the crappy C++11 support.

Edit 1: Changed the literal "Hello World" to be std::string("Hello World") to make an rvalue.

  • 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-27T09:26:59+00:00Added an answer on May 27, 2026 at 9:26 am

    First of all, read this to get a full idea of forwarding. (Yes, I’m delegating most of this answer elsewhere.)

    To summarize, forwarding means that lvalues stay lvalues and rvalues stay rvalues. You can’t do that with a single type, so you need two. So for each forwarded argument, you need two versions for that argument, which requires 2N combinations total for the function. You could code all the combinations of the function, but if you use templates then those various combinations are generated for you as needed.


    If you’re trying to optimize copies and moves, such as in:

    struct foo
    {
        foo(const T& pX, const U& pY, const V& pZ) :
        x(pX),
        y(pY),
        z(pZ)
        {}
    
        foo(T&& pX, const U& pY, const V& pZ) :
        x(std::move(pX)),
        y(pY),
        z(pZ)
        {}
    
        // etc.? :(
    
        T x;
        U y;
        V z;
    };
    

    Then you should stop and do it this way:

    struct foo
    {
        // these are either copy-constructed or move-constructed,
        // but after that they're all yours to move to wherever
        // (that is, either: copy->move, or move->move)
        foo(T pX, U pY, V pZ) :
        x(std::move(pX)),
        y(std::move(pY)),
        z(std::move(pZ))
        {}
    
        T x;
        U y;
        V z;
    };
    

    You only need one constructor. Guideline: if you need your own copy of the data, make that copy in the parameter list; this enables the decision to copy or move up to the caller and compiler.

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

Sidebar

Related Questions

C++0x shows an example of using std::forward : template<class T> void foo(T&& arg) {
Why is the following code invalid? template <typename S, typename T> struct B{ void
Is the following code valid? class Foo() { int* Bar; public: Foo() { *Bar
The following code yields an error error: ‘struct Foo’ is not a valid type
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
Why is this code not valid? #include <vector> template <typename T> class A {
I just written the following piece of code template<typename T> inline bool contains(T haystack,typename
Example: Is the following code valid against the JSON Spec ? { precision: "zip"
Can somebody explain why the following code is not valid? Is it because the
I'm getting the following error whenever my code creates a DataTableReader from a valid

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.