Reputation: 135
I recently started learning about rvalues,lvalues, move semantics and I can't understands some conceps like rvalue refs .. why can I assign a value to a rvalue ref like : int&& x = 10;
.. doesn't that mean that an rvalue ref is an lvalue(I tought that a rvalue ref is a rvalue)? If that is true ... what's the point of having rvalue references? If that is false then what is the difference between an rvalue ref and an lvalue ?
Upvotes: 8
Views: 1623
Reputation: 238351
why can I assign a value to a rvalue ref like : int&& x = 10
That's not an assignment. That's initialisation. Here, a temporary object is materialised from the integer constant and the reference is bound to that temporary. Same thing can be done with lvalue references to const: const int& x = 10
. It cannot be done with lvalue references to non-const since they cannot be bound to rvalues.
doesn't that mean that an rvalue ref is an lvalue
Only expressions have values. int&& x = 10;
is a declaration and not an expression. Thus the declaration doesn't have a value in the first place.
The declaration contains an expression 10
which is a prvalue.
Types are not expressions either, so "rvalue reference is an lvalue" is meaningless.
Variables don't have value categories either. However id-expressions - which are just expressions that name a variable - do have a value category. They are lvalues regardless of the type of the named variable - yes, even if the type is rvalue reference.
Upvotes: 7