为什么不可能在 C++ 中创建一个引用数组? [英] Why it is impossible to create an array of references in c++?

查看:38
本文介绍了为什么不可能在 C++ 中创建一个引用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++ 标准 8.3.2/4 说:

C++ Standard 8.3.2/4 says:

不得引用引用,没有引用数组,并且没有指向引用的指针.

There shall be no references to references, no arrays of references, and no pointers to references.

但我不明白为什么将这个限制添加到 C++ 中.在我看来,下面的代码可以很容易地编译和工作?这种限制的真正原因是什么?

But I can't understand why this restriction is added to c++. In my opinion the code bellow can easily be compiled and work? What is the real cause of this restriction?

int a = 10, b = 20;
int &c[] = {a, b};

推荐答案

因为数组的索引实际上是根据隐式转换为指针定义的,然后是指针运算.因此,要支持这一点,您还必须支持指向引用的指针,并定义指针算术对它们的含义.

Because indexation into an array is actually defined in terms of an implicit conversion to a pointer, then pointer arithmetic. So to support this, you'd have to also support pointers to references, and define what pointer arithmetic means on them.

这篇关于为什么不可能在 C++ 中创建一个引用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆