c++ - 引用引用在ISO C++ 2003标准中是不允许的,但为什么编译器允许呢?

标签 c++ visual-c++ gcc reference

根据 ISO C++ 2003 标准第 8.3.2 节

"references to references are not allowed"

但是我在 Visual C++ 和 Ideone 中尝试了以下代码,并且两个编译器都成功地运行了这段代码。

Ideone GCC C++4.3.2

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;
}

看到编译器的这种行为后,我真的很困惑;有人可以解释一下吗?

最佳答案

您没有在代码中创建引用对引用。

它只是另一个 int&,即两者都是对 int

的引用

(非法 C++03 引用的 T.C. shows an example 引用)


C++11 标准部分 § 8.3.2 通过示例明确说明了这一点(禁止引用引用,当然,在 C++03 和 C++11 之间没有改变,但是引用折叠在 C 中是新的++11):

If a typedef-name (7.1.3 , 14.1) or a decltype-specifier (7.1.6.2) denotes a type TR that is a reference to a type T, an attempt to create the type “lvalue reference to cv TR” creates the type “lvalue reference to T”, while an attempt to create the type “rvalue reference to cv TR” creates the type 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&

关于c++ - 引用引用在ISO C++ 2003标准中是不允许的,但为什么编译器允许呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24978819/

相关文章:

c# - 子语言 C#

visual-c++ - 区分单击和双击 C++

c - GCC linaro 编译器抛出错误 "unknown type name size_t"

c++ - char[5] 和 char[10] 有什么区别?

c++ - 公共(public)基类打破了元组的空基类优化

c++ - 我的 SFML 项目的主循环只发生一次。有人知道为什么吗?

c++ - "undefined reference"(适用于 linux,但不适用于 Windows 上的 cygwin)

c++ - 如何为 do {} while(false) 禁用 C4127

c - 将指针插入 GCC 中的 eax 和 ebx 寄存器

c - GCC 包含外部源文件的问题