Search code examples
c++c++11rvalue-reference

An rvalue reference binds to an lvalue


Let's considet the following code:

int x = 0;
using U = int&;
U&& r = x;

I tought that, by definition, an rvalue reference to Type only binds to an rvalue expressions of Type (no matter what type is equal to).

I have read some questions on SO, but couldn't figure out the meaning of that code (though, I know that there is a connection to the perfect forwarding). Am I right that the code fragment, I presented, is defined to be a valid code just to make the perfect forwarding possible?

And I'm aware of the reference collapsing rules, but their effect (the possibility to bind an rvalue reference to an lvalue expression) seems to contradict to the definition of an rvalue reference.

I disagree that this is a duplicate question - I'm asking about the concrete code fragment.


Solution

  • In this case, reference collapsing kicks in and U&&, fully expanded to int& &&, collapses to int&, which means you end up with lvalue reference. You are correct that rvalue reference cannot bind to an lvalue.