引用的定义相互矛盾
我正在学习 C++ 中的引用。特别是,我了解到引用不是实际对象。相反,它们引用了其他一些对象。也就是说,引用只是其他对象的别名。
然后我遇到了 this ,上面写着:
重要提示:尽管引用通常是使用底层汇编语言中的地址来实现的,但请不要将引用视为指向对象的滑稽指针。 引用就是对象,只是有另一个名称。它既不是指向对象的指针,也不是对象的副本。 它是对象。没有 C++ 语法可以让您独立于它所引用的对象对引用本身进行操作。
我知道上面的引用意味着我们无法将引用本身与它所引用的对象分开进行操作,但它仍然似乎暗示“引用是一个对象”。
另外,我遇到过下面给出的句子:
在 ISO C++ 中,引用不是对象。因此,它不需要任何内存表示。
我没有第二条引言的链接,但我在某处的一篇帖子中读到了它。
我的问题是,假设第二个引用也来自标准(可能并非如此),这两个引用的陈述是否相互矛盾。或者至少第一句话是有误导性的。哪一个是正确的。
我目前的理解(通过阅读《C++ Primer 5th Edition》等书籍)是引用是对象的别名。这让我想到它们不应该占用内存中的任何空间。
I am learning about references in C++. In particular, i have learnt that references are not actual objects. Instead they refer to some other object. That is, reference are just alias for other objects.
Then i came across this which says:
Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object, just with another name. It is neither a pointer to the object, nor a copy of the object. It is the object. There is no C++ syntax that lets you operate on the reference itself separate from the object to which it refers.
I get that the above quote means that we can't operate on the reference itself separate from the object to which it refers but it still seems to imply that "a reference is an object".
Also, i have come across the the sentence given below:
In ISO C++, a reference is not an object. As such, it needs not have any memory representation.
I don't have a link to this 2nd quote but i read it in one of SO's post somewhere.
My question is that assuming the second quote is also from the standard(which may not be the case), doesn't these 2 quoted statements contradict each other. Or at least the first quote is misleading. Which one is correct.
My current understanding(by reading books like C++ Primer 5th edition) is that references are an alias for objects. Which leads me to the thinking that they should not take any space in memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注释是非正式的,通常不应被解释为严格的规则。如果解释与标准规则相矛盾,那么该解释就是错误的。
引用和对象是不同类型的实体。引用并不是一个与其命名的对象不同的对象。不可能形成指向引用的指针。 “指向引用的指针”甚至不是有效的类型类别。
该注释试图说引用“是”它所命名的对象,因为使用引用就是使用所引用的对象。
引用占用空间或不占用空间。由语言实现来确定在每种情况下是否需要空间。
标准报价:
在标准规范之外,如果您想要使用空间的引用示例,请尝试向类中添加引用成员,您很可能会观察到引用成员的大小阶级增加。
指针确实在标准指定的抽象机中占用空间。但如果你从不观察存储,那么存储在实践中完全有可能不存在。引用和指针之间的一个显着区别是您无法直接观察引用的存储。
哲学家:“如果一棵树倒在抽象机器中,并且周围没有人观察它,它会产生影响吗?”
优化者:“如果我能帮助的话就不会。”
Notes are informal and usually should not to be interpreted as strict rules. If an interpretation contradicts with standard rules, then that interpretation is wrong.
References and objects are different kinds of entities. A reference is not an object distinct from the one that it names. It isn't possible to form a pointer to a reference. A "pointer to reference" isn't even a valid type category.
The note is trying to say that reference "is" the object which it names in the sense that using the reference is using the referred object.
References take space or they don't take space. It's up to the language implementation to figure out whether it needs space in each case.
Standard quote:
Outside of standard specifications, if you want an example of reference using space, try adding a reference member to a class and you are very likely to observe that the size of the class increases.
Pointers do take space in the abstract machine that the standard specifies. But if you never observe the storage, then it's entirely possible that the storage never exists in practice. A significant difference between references and pointers is that you cannot observe the storage of a reference directly.
Philosopher: "If a tree falls in an abstract machine and no one is around to observe it, does it have an effect?"
Optimiser: "Not if I can help it."
引用提供了另一种引用对象的方式。这在通过引用向函数传递参数时尤其有用。更正式地说,引用是绑定到变量的别名,在本例中包括匿名临时。
幸运的是,我们不需要关心它们是如何实现的。这是编译器的工作,并且技术各不相同。 C++ 标准不要求它们占用任何内存。
顺便说一下,有一种区分引用类型的方法。不可分离性实际上更多的是不能将引用绑定到任何其他变量。请
参阅最后注意
&a
和&ref
必须始终相同。A reference provides another way of referring to an object. That's useful particularly when passing parameters by reference to functions. More formally, a reference is an alias that binds to a variable including, in this case, an anonymous temporary.
Fortunately we don't need to concern ourselves how they are implemented. That's the job of the compiler, and techniques vary. The C++ standard does not require them to occupy any memory.
There is a way of distinguishing reference types by the way. Non-separability is really more about not being able to bind the reference to any other variable. See
Note finally that
&a
and&ref
must always be the same.第一条引用实际上是在说引用不能与对象分离。
它实际上意味着引用是对象的不可分离的、非一流的别名,正如您首先所说的那样。
这些讨论的困难在于“对象”的标准含义已经与大多数不太正式的上下文中使用的含义不同。
让我们从简单的开始:
int a;
通常会被描述为声明一个整数对象
a
,对吧?它实际上a
绑定到适当范围内的该对象现在,如果我们编写
int &b = a;
,我们可以说
b
是对象,就像我们可以说a
是对象一样。实际上两者都不正确,但考虑到非正式文本已经使用后者,这并不更糟。相反,我们应该说名称
b
与名称a
引用相同的对象。这与称其为别名完全一致,但如果在非正式或介绍性文本中到处写“...由名称a
引用的整数对象...”而不是仅仅写,就会显得相当麻烦。 “整数a
”。至于占用内存空间......这取决于。如果我为单个函数内的单个对象引入 100 个别名,如果编译器不只是折叠它们(尽管它们当然可能仍然显示在调试符号中),我会感到非常惊讶。删除冗余名称不会丢失任何信息。
如果我通过引用非内联函数传递参数,则正在传达一些实际信息,并且该信息必须存储在某处。
The first quote is really saying the reference is not separable from the object.
It really implies that a reference is a non-separable, non-first-class alias for an object, exactly as you first said.
The difficulty with these discussions is that the standardese meaning of "object" is already different from the meaning used in most less-formal contexts.
Let's start simple:
int a;
Would often be described as declaring an integer object
a
, right? It actuallya
to that object in the appropriate scopeNow, if we write
int &b = a;
we could say that
b
is the object in the same way as we could say thata
is the object. Actually neither are correct, but given that informal text already uses the latter, it's no worse.We should instead say that the name
b
refers to the same object as the namea
. This is exactly consistent with calling it an alias, but informal or introductory texts would seem pretty cumbersome if they wrote "... the integer object referred to by the namea
..." everywhere instead of just "the integera
".As for taking space in memory ... it depends. If I introduce 100 aliases for a single object inside a single function I'd be really surprised if the compiler didn't just collapse them (although of course they might still show up in debug symbols). No information is being lost here by eliding the redundant names.
If I pass an argument by reference to a non-inlined function, some actual information is being communicated, and that information must be stored somewhere.
引用实际上“是什么”并没有多大意义:您可以说它是引用的对象,或者它是它的别名,并且这些声明在某种意义上都是正确的。
让我们逐行浏览一下这个程序。
int a(0);
在堆栈上分配一些内存(通常是 4 个字节)来保存整数。int& ref(a);
不一定分配内存,它是否真的会分配是特定于编译器的。从这个意义上说,ref
本身并不是一个对象:它只是a
的别名,另一个名称。这就是第二句话“引用不是对象”的含义。 (请注意,有时,例如,当编译时无法知道引用的对象时,引用必须为对象的地址保留额外的空间。在这些情况下,引用只是指针的语法糖。)a
的值设置为 1。从这个意义上说,您可以将ref
视为与a
完全相同的对象。 对引用“进行”的任何操作实际上都会对引用的对象进行操作。这就是第一句话“它是对象”的含义。What a reference actually "is" doesn't make much sense: you could say it is the referenced object or that it is an alias to it, and these claims are both true in some sense.
Let's go through this program line by line.
int a(0);
allocates some memory (usually 4 bytes) on the stack to hold an integer.int& ref(a);
doesn't necessarily allocate memory, and wether it actually will is compiler-specific. In this sense,ref
itself is not an object: it is simply an alias, another name, fora
. This is what the second quote means by "a reference is not an object". (Please note that sometimes, when the what object is referenced can't be known at compile-time for example, a reference has to reserve additional space for the object's address. In these cases, references are just syntactic sugar for pointers.)ref = 1;
sets the value ofa
to one. In this sense, you can think ofref
as being precisely the same object asa
. Any operation "on" the reference will actually operate on the referenced object. This is what the first quote means by "It is the object".