01 / 21
L R

Lvalues vs.
Rvalues in C++

Understand the two fundamental value categories that govern how C++ handles memory, assignment, and expression evaluation.

C++

Why Do Value Categories Matter?

Every expression in C++ has a type and a value category. The type tells the compiler what data you have. The value category tells the compiler where that data lives and what you can do with it.

If you have ever seen an error like "cannot take address of an rvalue" or "cannot assign to a temporary", understanding lvalues and rvalues is the key to decoding it.

What Is an Lvalue?

An lvalue (historically "left value") is an expression that refers to a persistent memory location. It has an identifiable address that your program can use again and again.

Memory Location x = 42 0x7ffc003a Lvalue

What Is an Rvalue?

An rvalue (historically "right value") is an expression that does not persist beyond the statement that creates it. It is a temporary: no lasting address you can point to.

Temporary Value x + 1 No address! Rvalue

The Core Rule

Can you use & on it? Yes Lvalue Has a persistent address Can appear on the left of = x, arr[0], *ptr, obj.member No Rvalue Temporary, no address Appears on the right of = 42, x+1, func(), "hello" The & test is the simplest mental model for beginners

Common Examples of Each

Lvalues
Variables: x, name
Array elements: arr[2]
Dereferenced pointers: *ptr
Member access: obj.data
Prefix increment: ++x
Rvalues
Literals: 42, 3.14, true
Arithmetic results: x + 1
Function returns (by value): getVal()
Casts: static_cast<int>(y)
Postfix increment: x++

Left Side vs. Right Side of Assignment

Assignment Rule int x = 10; // x is lvalue, 10 is rvalue int y = x + 5; // y is lvalue, (x+5) is rvalue x + 5 = y; // ERROR: rvalue on left side!

However, do not think "lvalue = left side only." Lvalues can also appear on the right side. In y = x, the lvalue x sits on the right. It just means "read the value stored at x."

Lvalue References

An lvalue reference (declared with &) can only bind to an lvalue. This is the "regular" reference you already know.

Lvalue Reference int a = 10; int& ref = a; // OK: a is an lvalue int& bad = 42; // ERROR: 42 is an rvalue

Rvalue References (C++11)

C++11 introduced rvalue references (declared with &&) that can bind specifically to rvalues. This is the foundation of move semantics.

Rvalue Reference int a = 10; int&& rref = a + 5; // OK: (a+5) is an rvalue int&& bad = a; // ERROR: a is an lvalue

Reference Binding Chart

Which references bind to what? Lvalue Rvalue
\(\texttt{T\&}\)
✓ ✕
\(\texttt{const T\&}\)
✓ ✓
\(\texttt{T\&\&}\)
✕ ✓ ✓ = binds ✕ = does not bind

How to Identify Value Categories

1
Name test: Does the expression have a name (a declared variable, parameter, or member)? If yes, it is likely an lvalue.
2
Address test: Can you write &expr and have it compile? If yes, it is an lvalue. If no, it is an rvalue.
3
Persistence test: Does the value outlive the current statement? Named objects persist: lvalue. Temporaries die at the semicolon: rvalue.
4
Assignment test: Can it appear on the left side of = (for non-const types)? If yes: lvalue.

Example 1: Simple Assignments

Classify Each Expression int x = 5; // x: lvalue | 5: rvalue int y = x; // y: lvalue | x: lvalue (read) int z = x + y; // z: lvalue | x+y: rvalue x = z * 2; // x: lvalue | z*2: rvalue

Example 2: Function Returns

Return Types Matter int getValue() { return 42; } // returns by value int& getRef() { static int s=0; return s; } getValue(); // rvalue: temporary copy getRef(); // lvalue: refers to s getRef() = 7; // OK! Assigns 7 to s

Example 3: Tricky Cases

Surprise: Named Rvalue References Are Lvalues int a = 10; int&& rref = a + 5; // rref binds to the rvalue (a+5) // But inside this scope, 'rref' itself is... int& ref2 = rref; // OK! rref is an lvalue rref = 99; // OK! Can assign to it

Common Mistakes

✕ "Lvalue = left side only"
Lvalues can appear on either side of =. The name is historical, not a rule.
✕ "Variables are always lvalues"
Named variables are lvalues, but the result of an expression using them (like x + 1) is often an rvalue.
✕ "rvalue ref is an rvalue"
A named rvalue reference (like rref) is itself an lvalue. Only the binding rule is affected by &&.

Lvalue vs. Rvalue Summary

Property Lvalue Rvalue
Has a name? Usually yes No
Has an address? Yes (& works) No (& fails)
Persists after statement? Yes No (temporary)
Can assign to? Yes (if non-const) No
Binds to \(\texttt{T\&}\)? Yes No
Binds to \(\texttt{T\&\&}\)? No Yes
Binds to \(\texttt{const T\&}\)? Yes Yes
Example x, arr[0], *ptr 42, x+1, func()

Question 1 of 5

Given int x = 10;, which of the following expressions is an rvalue?

Question 2 of 5

Which statement will cause a compilation error?

Question 3 of 5

A function declared as int getValue() (returning by value). The expression getValue() is:

Question 4 of 5

Given \(\texttt{int\&\& rref = 5 + 3;}\), what is the value category of the expression rref itself?

Question 5 of 5

Which of the following is true about \(\texttt{const T\&}\)?