简体   繁体   中英

Why can i assign a value to a rvalue reference?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM