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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:40:19+00:00 2026-06-18T02:40:19+00:00

In C++11 , it is common practice to pass an lvalue into a function

  • 0

In C++11, it is common practice to pass an lvalue into a function by reference.

int& f(int& a){
    return a;
}

int main(void){
    auto a = 1;
    auto b = f(a);
    return 0;
}

However, is it possible to have a value passed into a function by rvalue reference and return this value by lvalue?

int& f(int&& a){
    return a;
}

int main(void){
    auto b = f(1);
    return 0;
}

Why is it or why isn’t it possible?

  • 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-06-18T02:40:21+00:00Added an answer on June 18, 2026 at 2:40 am

    It’s possible, but normally unwise. This code is OK:

    #include <utility>
    #include <iostream>
    
    int &foo(int &&a) {
        return a;
    }
    
    int main() {
        int a = 1;
        std::cout << foo(std::move(a)) << "\n";
    }
    

    This code is OK too:

    int main() {
        std::cout << foo(1) << "\n";
    }
    

    This code has undefined behavior:

    int main() {
        int &a = foo(1); // lifetime of the temporary 1 ends "at the semi-colon"
        std::cout << a << "\n";
    }
    

    So, it’s quite easy to misuse the function foo.

    As for the reason it works — all that’s happening here is an implicit conversion from an rvalue reference to an lvalue reference. It would be inconvenient if that were not permitted, because it would mean for example that you could not write:

    void bar1(const int &a) {
        std::cout << (a + 1) << "\n";
    }
    
    void bar2(int &&a) {
        bar1(a);
        ... do destructive stuff with a ...
    }
    

    There might be stronger reasons for permitting the implicit conversion. I don’t know the official motivation, this is just the first I thought of.

    Even in C++03 there was a related issue. You can write:

    const int *baz(const int &a) { return &a; }
    

    in order to take a pointer to a temporary/value — something that the language prevents you doing directly and which leads to the same undefined behavior if the return value outlives the expression in which it is produced.

    You could say that forbidding &1 (taking a pointer to a literal or other temporary) is one place in which the C++ standard doesn’t just assume the programmer knows what they’re doing. But I think historically the reasoning behind it is that you have to be able to take a const reference to a temporary in order for operator overloading to work. You don’t have to be able to take a pointer to a temporary, and C forbids it because in C an integer literal never needs to have a memory location. So C++ continues to forbid it, even though in C++ when you do take a reference to an integer literal then the compiler may be forced to create an actual location containing that value. Stroustrup probably could have said the same about taking a pointer to an integer literal, but there was no need.

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

Sidebar

Related Questions

Lets take qsort()'s comparison callback function as an example int (*compar)(const void *, const
Here is a common practice in JavaScript: (function($) { ...code... })(jQuery); I understand the
Common Situations: Passing std::string to a function foo(std::string*) or foo(std::string&); Passing tr1::shared_ptr to a
A common practice to avoid race conditions (in multi-threaded apps) when triggering events is
It's common practice for tables of regression outcomes in academic papers to have a
It's at least common practice to treat null BSTR (null WCHAR* pointer) as an
Here's a common practice I see often (including from a very popular iPhone developer
What's the common practice to deal with Integer overflows like 999999*999999 (result > Integer.MAX_VALUE)
What is common practice given the two xml formats? And how would you go
In Java IoC / DI is a very common practice which is extensively used

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.