无法从可转换类型初始化非常量引用
我无法从可转换类型 T2 初始化对类型 T1 的非常量引用。但是,我可以使用 const 引用。
long l;
const long long &const_ref = l; // fine
long long &ref = l; // error: invalid initialization of reference of
// type 'long long int&' from expression of type
// 'long int'
我遇到的大多数问题都与无法分配给非常量引用的右值有关。这里的情况并非如此——有人能解释一下吗?谢谢。
I cannot initialize a non-const reference to type T1 from a convertible type T2. However, I can with a const reference.
long l;
const long long &const_ref = l; // fine
long long &ref = l; // error: invalid initialization of reference of
// type 'long long int&' from expression of type
// 'long int'
Most problems I encountered were related to r-values that cannot be assigned to a non-const reference. This is not the case here -- can someone explain? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
整数提升会产生右值。
long
可以提升为long long
,然后绑定到 const 引用。就像您已经完成的那样:相反,如您所知,右值不能绑定到非常量引用。 (毕竟没有实际的
long long
可以引用。)An integer promotion results in an rvalue.
long
can be promoted to along long
, and then it gets bound to a const reference. Just as if you had done:Contrarily an rvalue, as you know, cannot be bound to a non-const reference. (After all, there is no actual
long long
to refer to.)long long
的大小不一定等于long
,甚至可能使用完全不同的内部表示形式。因此,您不能将对long
的非常量引用绑定到long long
类型的对象,反之亦然。标准禁止它,并且您的编译器不允许它是正确的。您可能会对以下代码片段有同样的疑问:
最后的初始化将不起作用。仅仅因为一种类型可以转换为另一种类型,并不意味着复合其中一种类型的另一种类型可以转换为复合另一种类型的第三种类型。同样,对于以下情况
在这种情况下
A
和B
分别复合类型long
和long long
,很像long&
和long long&
复合这些类型。然而,仅仅因为这个事实,它们就不能相互转换。其他规则也适用。如果引用是 const,则会创建一个具有正确类型的临时对象,然后将该引用绑定到该对象。
long long
is not necessarily sized equal tolong
and may even use an entire different internal representation. Therefor you cannot bind a non-const reference tolong
to an object of typelong long
or the other way around. The Standard forbids it, and your compiler is correct to not allow it.You can wonder the same way about the following code snippet:
The last initialization won't work. Just because a type is convertible to another one, doesn't mean another type that compounds one of them is convertible to a third type that compounds the other one. Likewise, for the following case
In this case
A
andB
each compound the typelong
andlong long
respectively, much likelong&
andlong long&
compound these types. However they won't be convertible into each other just because of that fact. Other rules happen to apply.If the reference is to const, a temporary object is created that has the correct type, and the reference is then bound to that object.
我不是标准律师,但我认为这是因为
long long
比long
更宽。允许使用 const 引用,因为您不会更改l
的值。常规引用可能会导致对于l
来说太大的赋值,因此编译器不会允许它。I'm not a standards lawyer, but I think this is because
long long
is wider thanlong
. A const reference is permitted because you won't be changing the value ofl
. A regular reference might lead to an assignment that's too big forl
, so the compiler won't allow it.让我们假设这是可能的:
这意味着稍后在代码中您可以将 ref 引用的值更改为大于 long 类型可以保存但可以用于 long long 的值。实际上,这意味着您覆盖了额外的内存字节,这些内存字节可以由不同的变量使用,从而产生不可预测的结果。
Let's assume that it's possible :
It means that later in the code you can change the value referenced by ref to the value that is bigger then long type can hold but ok for long long. Practically, it means that you overwrite extra bytes of memory which can be used by different variable with unpredictable results.