Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assigning Rvalue reference to Lvalue reference

Tags:

c++

c++11

c++14

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

How is this possible?

Is this related to "reference collapsing rule"?

like image 816
suhdonghwi Avatar asked Jun 18 '16 06:06

suhdonghwi


1 Answers

 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.

like image 128
Nawaz Avatar answered Sep 21 '22 21:09

Nawaz