Neh*_*eha 6 c++ gcc reference visual-c++
根据ISO C++ 2003标准第8.3.2节
"不允许引用引用"
但我尝试在Visual C++和Ideone中使用以下代码,两个编译器都成功运行此代码.
int main()
{
int i=2;
int &ref_i=i;
int &ref_ref_i=ref_i; // should be an error according to c++ 2003 standard
cout<<i<<endl<<ref_i<<endl<<ref_ref_i;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看完编译器的这种行为后我真的很困惑; 有人能解释一下吗?
qua*_*dev 14
您不在代码中创建引用引用.
它只是另一个int&,即两者都是对a的引用int
C++ 11标准第§8.3.2节通过一个例子明确地说明了这一点(不允许引用引用,当然,在C++ 03和C++ 11之间没有变化,但参考折叠在C++ 11中是新的):
如果typedef-name(7.1.3,14.1)或decltype-specifier(7.1.6.2)表示类型TR是对类型T的引用,则尝试创建类型"对cv TR的左值引用"会创建键入"左值引用T",而尝试创建类型"对cv TR的右值引用"会创建类型TR.
int i;
typedef int& LRI;
typedef int&& RRI;
LRI& r1 = i; // r1 has the type int&
const LRI& r2 = i; // r2 has the type int&
const LRI&& r3 = i; // r3 has the type int&
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
478 次 |
| 最近记录: |