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

  • Home
  • SEARCH
  • 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 3622730
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:17:45+00:00 2026-05-18T23:17:45+00:00

When programming in C++03, we can’t pass an unnamed temporary T() to a function

  • 0

When programming in C++03, we can’t pass an unnamed temporary T() to a function void foo(T&);. The usual solution is to give the temporary a name, and then pass it like:

T v;
foo(v);

Now, along comes C++0x – and now with rvalue references, a function defined as void foo(T&&) will allow me to pass a temporary. Which brings me to my question: since a function that takes an rvalue reference can take both rvalue references (unnamed temporaries) as well as lvalue references (named non-const references), is there any reason to use lvalue references anymore in function parameters? Shouldn’t we always use rvalues as function parameters?

Granted, a function that takes an lvalue reference would prevent the caller from passing a temporary, but I’m not sure if that’s a useful restriction.

  • 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-18T23:17:45+00:00Added an answer on May 18, 2026 at 11:17 pm

    “since a function that takes an rvalue reference can take both rvalue references (unnamed temporaries) as well as lvalue references (named non-const references)”

    This is an incorrect statement. During the first iterations of the rvalue reference specification this was true, but it no longer is and is implemented at least in MSVC to comply with this later change. In other words, this is illegal:

    void f(char&&);
    
    char x;
    f(x);
    

    In order to call a function expecting rvalue references with an lvalue you must turn it into an rvalue like so:

    f(std::move(x))
    

    Of course, that syntax makes it quite clear what the difference between a function taking an lvalue reference and one taking an rvalue reference really is: an rvalue reference is not expected to survive the call. This is a big deal.

    Now, you can of course make up a new function that does exactly what std::move does and then you “can” use rvalue references sort of like lvalue references. I thought about doing this for instance with a visitor framework I have when sometimes you simply don’t care about any result of the visitor call, but other times you do and thus need an lvalue reference in those cases. With an rvalue reference I could get both…but it’s such a violation of the rvalue reference semantics that I decided it was a bad idea.

    Your statement may be a confusion based upon this:

    template < typename T >
    void f(T&&);
    
    char x;
    f(x);
    

    This works, but not because you are passing an lvalue as an rvalue reference. It works because of reference decay (also new in C++0x). When you pass an lvalue to such a template it actually gets instantiated like so:

    void f<char&>(char&&&);
    

    Reference decay says that &&& turns into & so then the actual instantiation looks like this:

    void f<char&>(char&);
    

    In other words, you’re simply passing an lvalue by reference…nothing new or special about that.

    Hope that clears things up.

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

Sidebar

Related Questions

I wonder if the objective function of a general dynamic programming problem can always
Why in java (I dont know any other programming languages) can an identifier not
I've got another question regarding to basic Android programming: How can I access the
Can anyone suggest me a helpful programming language which can be used to create
Using the WCF web programming model one can specify an operation contract like so:
What do you call a programming language that can execute its own code (passed
I'm just starting PHP programming as you can tell. I want the results of
There is so many option in each programming languages which can be mentioned in
Is there any really low level programming language that can get access the memory
I can enumerate many features of functional programming, but when my friend asked me

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.