assigning Rvalue reference to Lvalue reference

ぐ巨炮叔叔 提交于 2020-01-14 09:47:28

问题


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

How is this possible?

Is this related to "reference collapsing rule"?


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/37894068/assigning-rvalue-reference-to-lvalue-reference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!