在过去的几天我一直在试图把握背后的左值/右值引用一个明显琐碎的原则。 让我们定义一个新的右值引用:
int&& x = 12;
x
因此是类型的左值表达式int&&
。 因为x
是一个左值,它可以被绑定到相同的类型,即,类型的左值参考的左值参考int&&
。 这样的左值参考将被定义为:
int&& & ref_x = x; // non-working code, just for the sake of explanation
当然,这是不可能明确地定义一个参照参考,并执行绑定是如下正确的方法:
int& ref_x = x;
C ++入门报告有关使用引用作为初始化如下:
当我们使用作为参考的初始化,我们实际上使用对象到该参考是结合
在另一方面,左值参考必须的左值表达式的类型相匹配。 我在想什么? 是参考压扁涉案?
谢谢。
No, x
(as an expression) is an expression of type int
. The type of the value of an expression is never a reference. In fact, x
is also an lvalue, since it is a named thing.
Also, there are no references to references, for the same reason: References bind to values, and values are never references.
If you're ever confused, just keep telling yourself: The value of an expression is always an object type. Whether the value category of an expression is l or r only determines what sort of things the value can bind to; it has no effect on its type.