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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:42:35+00:00 2026-05-24T19:42:35+00:00

Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding

  • 0

Possible Duplicate:
What does T&& mean in C++11?

For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example:

void myFunction(int&& val);     //what does this mean?!

I understand the idea of passing-by-reference, so

void addTwo(int& a)
{
    a += 2;
}

int main()
{
    int x = 5;
    addTwo(x);

    return 0;
}

works and is intuitive to me.

  • 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-24T19:42:35+00:00Added an answer on May 24, 2026 at 7:42 pm

    This is not a reference of a reference, but rather a new language feature called an rvalue reference that represents (informally) a reference to an object in memory that isn’t referenced elsewhere in the program and can be destructively modified. For example, the return value of a function can be captured by an rvalue reference, as can temporary values introduced into expressions.

    Rvalue references can be used for a variety of purposes. From the perspective of most C++ programmers, they can be used to implement move semantics, whereby a new object can be initialized by "moving" the contents of an old object out of the old object and into a new object. You can use this to return huge objects from functions in C++11 without paying a huge cost to copy the object, since the object used to capture the return value can be initialized using the move constructor by just stealing the internals from the temporary object created by the return statement.

    Move semantics are orthogonal to copy semantics, so objects can be movable without being copyable. For example, std::ofstreams are not copyable, but they will be movable, so you could return std::ofstreams from functions using the move behavior. This currently cannot be done in C++03. For example, this code is illegal in C++03 but perfectly fine (and encouraged!) in C++11:

    std::ifstream GetUserFile() {
        while (true) {
            std::cout << "Enter filename: ";
            std::string filename;
            std::getline(std::cin, filename);
    
            ifstream input(filename); // Note: No .c_str() either!
            if (input) return input;
    
            std::cout << "Sorry, I couldn't open that file." << std::endl;
        }
    }
    
    std::ifstream file = GetUserFile(); // Okay, move stream out of the function.
    

    Intuitively, a function that takes an rvalue reference is a function that (probably) is trying to avoid an expensive copy by moving the contents of an old object into a new object. For example, you could define a move constructor for a vector-like object by having that constructor take in an rvalue reference. If we represent the vector as a triple of a pointer to an array, the capacity of the array, and the used space, we might implement its move constructor as follows:

    vector::vector(vector&& rhs) {
        /* Steal resources from rhs. */
        elems    = rhs.elems;
        size     = rhs.size;
        capacity = rhs.capacity;
    
        /* Destructively modify rhs to avoid having two objects sharing 
         * an underlying array.
         */
        rhs.elems    = nullptr; // Note use of nullptr instead of NULL
        rhs.size     = 0;
        rhs.capacity = 0;
    }
    

    It’s important to notice that when we clear out rhs at the end of the constructor that we end up putting rhs into such a state that

    1. Will not cause a crash when its destructor invokes (notice that we set its element pointer to nullptr, since freeing nullptr is safe), and
    2. Still lets the object be assigned a new value. This latter point is tricky, but it’s important to ensure that you can still give the cleared-out object a new value at some point. This is because it is possible to obtain an rvalue reference to an object that can still be referenced later in the program.

    To shed some light on (2), one interesting use case for rvalue references is the ability to explicitly move values around between objects. For example, consider this idiomatic implementation of swap:

    template <typename T> void swap(T& lhs, T& rhs) {
        T temp = lhs;
        lhs = rhs;
        rhs = temp;
    }
    

    This code is legal, but it’s a bit unusual. In particular, it ends up making three copies – first when setting temp equal to a copy of lhs, once setting lhs to be a copy of rhs, and once setting rhs to be a copy of temp. But we don’t really want to be making any copies at all here; instead, we just want to shuffle the values around. Consequently, in C++11, you’ll be able to explicitly get rvalue references to objects by using the std::move function:

    template <typename T> void swap(T& lhs, T& rhs) {
        T temp = std::move(lhs);
        lhs = std::move(rhs);
        rhs = std::move(temp);
    }
    

    Now, no copies are made at all. We move the contents of lhs into temp, then move the contents of rhs into lhs, then moves the contents of temp into rhs. In doing so, we left both lhs and rhs in an "emptied" state temporarily before putting new values into them. It’s important that when writing the code to move the contents out of an object that we leave the object in a somewhat well-formed state so that this code works correctly.

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

Sidebar

Related Questions

Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
Possible Duplicate: What does map(&:name) mean in Ruby? In Ruby, I know that if
Possible Duplicate: What does map(&:name) mean in Ruby? Ruby/Ruby on Rails ampersand colon shortcut
Possible Duplicate: What does map(&:name) mean in Ruby? I came across a code snippet
Possible Duplicate: What does map(&:name) mean in Ruby? Post.all.map(&:id) will return => [1, 2,
Possible Duplicates: Reference - What does this symbol mean in PHP? what do “=&”
Possible Duplicate: Reference - What does this symbol mean in PHP? The third line
Possible Duplicate: Reference - What does this symbol mean in PHP? Hi, I was
Possible Duplicate: What does T&& mean in C++0x? I had never seen a double
Possible Duplicate: What does map(&:name) mean in Ruby? What are things like survey.map(&:questions).flatten.compact called,

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.