An lvalue is an expression that refers to an object you can name and find in memory, such as a variable. Lvalues can appear on the left side of an assignment, as long as the object can be changed. The idea is simple once you see a few examples, and it explains a lot of compiler messages.
Where the name comes from
The name is usually explained as “left value,” because lvalues were the things that could stand on the left of an assignment. That’s a useful memory aid, but it’s only a rough guide. Modern C++ splits the categories more finely, and some lvalues can’t be assigned to at all, such as const variables and arrays. For everyday code, the split between lvalues and rvalues is enough.
Lvalues and rvalues
int x = 5; // x is an lvalue; 5 is an rvalue
x = 10; // fine: x is an lvalue on the left
// 10 = x; // error: 10 is an rvalue
An rvalue is a value that doesn’t name a place you can write to. The literal 5 is one. So is the result of x + 1, which is a new value that doesn’t live in a named variable. You can read an rvalue, but you can’t assign to it.
A quick test with &
A handy rule of thumb is that if you can put & in front of an expression and get its address, it’s an lvalue:
int x = 5;
int* p = &x; // fine: x is an lvalue
// int* q = &5; // error: 5 has no address to take
This is a rule of thumb, not the full definition. A const variable has an address too, and it’s still an lvalue, but you can’t assign to it. So use the test to see whether something is a place, then check whether that place can be changed.
Lvalues returned from functions
A function’s return type decides whether its result is an lvalue. Returning a reference gives you a place. Returning a value gives you a copy.
#include <vector>
int& first(std::vector<int>& values) {
return values[0]; // returns a reference, so the result is an lvalue
}
int copy_first(std::vector<int>& values) {
return values[0]; // returns a copy, so the result is an rvalue
}
int main() {
std::vector<int> nums = {1, 2, 3};
first(nums) = 42; // fine: nums[0] is now 42
// copy_first(nums) = 42; // error: the result is a copy
}
Both functions look alike, but only the first result can appear on the left side of =. The first version assumes the vector isn’t empty.
Lvalue references
Lvalues also matter for references. A reference declared with &, such as int& r = x;, binds to an lvalue. A const reference can also bind to an rvalue, as in const int& r = 5;. That’s why a function that takes const int& can accept a literal, while a function that takes int& can’t.
Lvalues you use every day
You use lvalues constantly, often without noticing. These expressions are all lvalues:
- A variable, such as
count - An array element, such as
scores[2] - A member of an object, such as
player.score - A dereferenced pointer, such as
*ptr
You can assign to each of them as long as they aren’t const. The dereferenced case is covered in What Does char* Do in C++?
When the term shows up in errors
Compilers use the word when an assignment or an increment is applied to something that isn’t a modifiable lvalue. If that’s what you’re seeing, Fixing the Expression Must Be a Modifiable Lvalue Error in C++ walks through the common causes and their fixes.
Summary
An lvalue is something with a location you can name and write to. An rvalue is a value that doesn’t have one. Assignment needs a modifiable lvalue on the left, which means the object can’t be const.