这是由Scott Meyers的C++ 11注示例代码,auto &&做什么?
int x;
auto&& a1 = x; // x is lvalue, so type of a1 is int&
auto&& a2 = std::move(x); // std::move(x) is rvalue, so type of a2 is int&&
我无法理解
auto&&
。
我有auto
一定的了解,从中我会说,auto& a1 = x
应的a1
类型int&
从引用代码,似乎是错误的。
我写了这个小代码,并在gcc下运行。
#include <iostream>
using namespace std;
int main()
{
int x = 4;
auto& a1 = x; //line 8
cout << a1 << endl;
++a1;
cout << x;
return 0;
}
输出= 4 (newline) 5
然后我修改线8 auto&& a1 = x;
,跑。相同的输出。
我的问题:是auto&
等于auto&&
?
如果它们不同,auto&&
做什么?
如果rhs是一个左值,我认为'auto&'和'auto &&'是等价的。如果rhs是一个右值,'auto&'会给你一个错误。 – balki 2012-02-06 15:52:46
[左值引用和右值引用相似](http://msdn.microsoft.com/en-us/library/f90831hc.aspx) – AJG85 2012-02-06 15:54:15
'auto &&'变量与函数模板中的T &&参数相同。 – fredoverflow 2012-02-06 17:13:46