Uri London
Uri London

Reputation: 10797

modifying the value of rvalue reference - How does it work?

The code below compiles and runs just fine. Just when I thought I'm starting to get a decent grasp on rvalue reference and std::forward - this very simple code uncovers there is something very fundamental about rvalue I don't understand. Please clarify.

#include <iostream>
#include <iomanip>
using namespace std;

void fn( int&& n )
{
    cout << "n=" << n << endl;
    n = 43;
    cout << "n=" << n << endl;
}


int main( )
{
    fn( 42 );
}

I compile it with g++ 4.7 with the following command line:
g++ --std=c++11 test.cpp

The output is:
n=42
n=43

My main issue is where does the compiler store the 'n' within the function fn?

Upvotes: 17

Views: 2791

Answers (2)

Andriy
Andriy

Reputation: 8594

I can tell some details of what happens here on the low level.

  1. A temporary variable of type int is created on the stack of main. It's assigned with value 42.

  2. The address of the temporary is passed to fn.

  3. fn writes 43 by that address, changing the value of the temporary.

  4. The function exits, the temporary dies at the end of the full expression involving the call.

Upvotes: 7

CashCow
CashCow

Reputation: 31435

Takes its address, i.e. &n and you will see its pointer value.

You can stick a local variable into your function and also in main and take their addresses too and see where that is, but relying on any comparison between them would be undefined behaviour.

That the int is not const is correct, your function acquires it. In the same way you can initialise a class with a collection using an r-value reference and then class can modify it later, i.e. it doesn't have to be a const member. However there will be no copy.

The object that is "moved" will, by the standard, be in a stable, usable state, but the actual value it holds is undefined.

Upvotes: 2

Related Questions