Uri*_*Uri 12 c++ rvalue-reference c++11
下面的代码编译并运行得很好.正当我以为我开始对rvalue引用和std :: forward进行了很好的掌握 - 这个非常简单的代码揭示了rvalue有一些非常基本的东西,我不明白.请澄清.
#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 );
}
Run Code Online (Sandbox Code Playgroud)
我用g ++ 4.7使用以下命令行编译它:
g ++ --std = c ++ 11 test.cpp
输出为:
n = 42
n = 43
我的主要问题是编译器在函数fn中存储'n'的位置?
我可以告诉一些细节,这里发生在低层次上的事情。
类型的临时变量int在 的堆栈上创建
main。它被赋值为 42。
临时地址被传递给fn.
fn 通过该地址写入 43,更改临时值。
函数退出,临时在涉及调用的完整表达式结束时死亡。