将右值引用分配给左值引用 [英] assigning Rvalue reference to Lvalue reference

查看:67
本文介绍了将右值引用分配给左值引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int&& rv = 10;
int& lv = rv; //no error

这怎么可能?

这与引用折叠规则有关吗?

Is this related to "reference collapsing rule"?

推荐答案

 int&& rv = 10; 
 int& lv = rv; //no error

首先,以命名的对象为从不一个右值。其次,由于 rv named 对象,因此即使绑定到rvalue,它也不是右值。由于 rv 是左值,因此可以毫无问题地绑定到左值。

First of all, a named object is never an rvalue. Second, since rv is named object, it is not a rvalue, even though it binds to rvalue. Since rv is lvalue, it can bind to lvalue without any problem.

请注意,rvalue-ness是表达式的属性,而不是变量。在上面的示例中,从 10 中创建了一个右值,并将其绑定到 rv ,正如我所说的,它是左值

Note that rvalue-ness is a property of an expression, not a variable. In the above example, an rvalue is created out of 10 and binds to rv, which as I said, is lvalue.

这篇关于将右值引用分配给左值引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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