0Pricing
C++ Academy · Lesson

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 — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 reference

Rvalues

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 rvalue

Why 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 literal

Binding 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 ref

Why 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.

Frequently asked questions

Is the “Lvalue vs Rvalue The Distinction” lesson free?

Yes — the full text of “Lvalue vs Rvalue The Distinction” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Lvalue vs Rvalue The Distinction
  2. Rvalue References and the && Syntax
  3. std::move and std::forward
  4. Move Constructors and Move Assignment
← Back to C++ Academy