Lvalue vs Rvalue The Distinction
Reason about value categories: lvalues, prvalues and xvalues.
Lvalue vs Rvalue The Distinction is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Two Worlds: Lvalue and Rvalue
Every expression in C++ is either an lvalue (a thing with a name and an address) or an rvalue (a temporary that has no name).
Lvalues
An lvalue refers to a persistent object — you can take its address with &.
int x = 42; // x is an lvalue
int* p = &x; // taking the address of an lvalue
int& r = x; // r is an lvalue referenceRvalues
An rvalue is a temporary — literals, function return values, expressions like a + b. Cannot take its address.
int a = 5, b = 3;
int c = a + b; // (a + b) is an rvalue
// int* p = &(a + b); // ERROR: cannot take address of an rvalueWhy the Distinction Matters
An rvalue is about to be destroyed. We can safely steal its resources rather than copy them — that is the basis of move semantics.
Three Categories Since C++11
Modern C++ refines the model:
- lvalue — has a name and identity
- prvalue (pure rvalue) — pure temporary (a literal, return-by-value)
- xvalue (expiring) — about to die (the result of
std::move)
lvalue vs rvalue References
An lvalue reference uses &; an rvalue reference uses &&.
int x = 10;
int& lr = x; // lvalue reference
int&& rr = 42; // rvalue reference, binds to literalBinding Rules
Type compatibility:
- lvalue reference (
T&) binds to lvalues - const lvalue reference (
const T&) binds to both - rvalue reference (
T&&) binds only to rvalues (and xvalues)
Checking with decltype
You can ask the compiler what category an expression is, by using decltype on it.
int x = 10;
static_assert(std::is_lvalue_reference<decltype((x))>::value);
// the extra parentheses make decltype yield the lvalue refWhy C++11 Added Rvalue References
To enable move semantics: a constructor that takes T&& can take ownership of a temporary s internal data, avoiding a deep copy.
Common Mistake: Named rvalue ref is an lvalue
An rvalue reference variable, once declared, has a name — making it an lvalue. To pass it on as an rvalue, use std::move.
void f(std::string&& s) {
// s here is an lvalue!
process(std::move(s)); // now it is an rvalue again
}Perfect Forwarding Preview
Templates that take T&& deduce either an lvalue or rvalue reference. Combined with std::forward, they can preserve the value category — covered in detail later.
Quick Check
Which of these expressions is an rvalue?
Recap
An lvalue has a name and address; an rvalue is a temporary. T& binds to lvalues, T&& binds to rvalues. This distinction enables move semantics — avoiding unnecessary copies of temporaries.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Lvalue vs Rvalue The Distinction” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Lvalue vs Rvalue The Distinction”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Lvalue vs Rvalue The Distinction”?
Reason about value categories: lvalues, prvalues and xvalues. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Lvalue vs Rvalue The Distinction” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Lvalue vs Rvalue The Distinction
- Rvalue References and the && Syntax
- std::move and std::forward
- Move Constructors and Move Assignment