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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:48:39+00:00 2026-06-12T22:48:39+00:00

The code below compiles and runs just fine. Just when I thought I’m starting

  • 0

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?

  • 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-12T22:48:40+00:00Added an answer on June 12, 2026 at 10:48 pm

    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.

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

Sidebar

Related Questions

Consider the code below which compiles and runs without error in Delphi 6. When
The code below compiles/runs without errors. It created the db which has the table
I can't get the code below to compile (see errors). Advice on correction would
This code compiles and runs with either a public implicit bool cast (commented out
The code compiles fine and I have included the vssapi.lib in the additional dependencies
Can anyone beat the performance of my integer to std::string code, linked below? There
It seems like I have the correct code, and it compiles, runs, and builds.
My program compiles and runs correctly but after the execution, my program crashes. Below
I have a small problem with a code written below. VS 2010 compiles it,
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with

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.