【发布时间】:2013-10-01 06:03:55
【问题描述】:
我正在尝试理解右值引用和移动语义。在下面的代码中,当我将 10 传递给 Print 函数时,它会调用右值引用重载,这是预期的。但究竟会发生什么,这 10 个将被复制到哪里(或从哪里引用)。其次std::move实际上做了什么?它是否从i 中提取值 10 然后传递它?或者是编译器使用右值引用的指令?
void Print(int& i)
{
cout<<"L Value reference "<<endl;
}
void Print(int&& i)
{
cout<<"R Value reference "<< endl;
}
int main()
{
int i = 10;
Print(i); //OK, understandable
Print(10); //will 10 is not getting copied? So where it will stored
Print(std::move(i)); //what does move exactly do
return 0;
}
谢谢。
【问题讨论】:
-
我推荐观看Scott Meyers talk at Going Native 2013。他详细解释了
std::move和std::forward。
标签: c++ c++11 move-semantics rvalue-reference