C++11/C++14 4. rvalue and lvalue - 2020
"If you're an experienced C++ programmer and are anything like me, you initially
approached C++11 thinking, "Yes, yes, I get it. It's C++, only more so." But as you
learned more, you were surprised by the scope of the changes. auto declarations,
range-based for loops, lambda expressions, and rvalue references change the face of
C++, to say nothing of the new concurrency features. And then there are the
idiomatic changes. 0 and typedefs are out, nullptr and alias declarations are in.
Enums should now be scoped. Smart pointers are now preferable to built-in ones.
Moving objects is normally better than copying them.
- Effective Modern C++ by Scott Meyers
The crude definition for lvalue and rvalue might be:
- lvalue : A value that resides in memory (heap or stack) and addressable.
- rvalue : A value that's not lvalue.
It resides only on the right side of an assignment expression such as a literal or a temporary which is intended to be non-modifiable.
"C++11's most pervasive feature is probably move semantics, and the foundation of move semantics is distinguishing expressions that are rvalue from those that are lvalues. That's because rvalues indicate objects eligible for move operations, while lvalues generally don't. In concept (though not always in practice), rvalue correspond to temporary objects returned from functions, while lvalues correspond to objects you can refer to, either by name or by following a pointer or lvalue reference."
"A useful heuristic to determine whether an expression is an lvalue is to ask if you can take its address. If you can, it typically is. If you can't, it's usually an rvalue."
- Effective Modern C++Most of the variables are lvalues, and here are examples of lvalues:
// lvalue examples int i = 7; // i: lvalue int *pi = &i; // i is addressable i = 10; // we can modify it class cat {}; cat c; // c is an lvalue for a user defined type
Then, what are rvalues?
// rvalue examples int i = 7; // i: lvalue but 7 is rvalue int k = i+3; // (i+3) is an rvalue int *pi = &(i + 3); // error, it's not addressable i + 3 = 10; // error - cannot assign a value to it 3 = i; // error - not assignable class cat {}; c = cat(); // cat() is an rvalue
How about the return value from a function:
int square(int x) { return x*x; } int sq = square(10); // square(10) is an rvalue
How about a reference, is it a rvalue or a lvalue?
The references that we've been using are lvalue references - references to lvalues. The lvalues references can only be bound to lvalues but not rvalues.
int i; int &r; = i; int &r; = 7; // error: lvalue cannot be bound to rvalue 7
However, we can bind an rvalue to a const lvalue reference (const reference):
const int&r; = 7; // OK
It's legal since when a compiler sees const, it converts 7 to an lvalue, and then assign it to the reference.
The rule "lvalue references can only be bound to lvalues but not rvalues" equally applies to an argument to a function:
int square(int& x) { return x*x; } int i = 7; square(i); // OK square(7); // error, 7 is an rvalue and cannot be assigned to a reference
Is there a way to pass 7 to the square function? Seems I've seen it working.
Yes, we can. The const is here for us!
int square(const int& x) { return x*x; } square(i); // OK square(7); // OK
C++11/C++14 Thread Tutorials
C++11 1. Creating Threads
C++11 2. Debugging with Visual Studio 2013
C++11 3. Threading with Lambda Function
C++11 4. Rvalue and Lvalue
C++11 5. Move semantics and Rvalue Reference
C++11 5B. Move semantics - more samples for move constructor
C++11 6. Thread with Move Semantics and Rvalue Reference
C++11 7. Threads - Sharing Memory and Mutex
C++11 8. Threads - Race Conditions
C++11 9. Threads - Deadlock
C++11 10. Threads - Condition Variables
C++11 11. Threads - unique futures (std::future<>) and shared futures (std::shared_future<>).
C++11 12. Threads - std::promise
C++11/C++14 New Features
initializer_list
Uniform initialization
Type Inference (auto) and Range-based for loop
The nullptr and strongly typed enumerations
Static assertions and Constructor delegation
override and final
default and delete specifier
constexpr and string literals
Lambda functions and expressions
std::array container
Rvalue and Lvalue (from C++11 Thread tutorial)
Move semantics and Rvalue Reference (from C++11 Thread tutorial)
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization