這是由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