C++
C++ Lvalue Required Assignment Fix
An assignment writes into a place - a variable, an element, the object a pointer or reference names. The left side of the failing assignment names only a value: a number, a returned copy, a computed result. The value exists, but there is no storage behind it for the new value to go into, and that is exactly what the compiler reports. This guide separates the five places this error most often hides, reads what the diagnostic actually reports, and matches each shape to the variable or reference that gives the assignment a real target.
Written November 24, 2026 · Tested with GCC 13.3.0 · 14 min read
- The rule underneath: the left side of an assignment must name an object
- How to read the diagnostic
- The first failure: assigning to a literal
- The second failure: assigning to a function that returns by value
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
Every assignment has two sides with different jobs: the right side produces a value, and the left side names the place that value is stored in. When the left side names only a value - a literal, a copy returned from a function, the result of a computation - the assignment has nowhere to write, and the compiler stops it with "lvalue required as left operand of assignment". The fix is never on the right side; it is to give the left side a real target, or to store the right side somewhere else.
The five forms in this guide each reproduce the failure with a small, complete program, and the left side is a value-only expression in five different ways: a literal written as the target, a call to a function that returns by value, a sum in parentheses, a cast applied to a variable, and a postfix increment. In each family the failing program is small, the full compiler output is shown, and the fix names an actual object - a variable declared for the purpose, the original variable under the cast, the variable the increment was applied to - and assigns to that. Three neighboring shapes in the traps fail with different errors, not this one: a const variable is an lvalue but read-only, an array name cannot be assigned to at all, and a value-only expression under ++ reports the increment instead of the assignment. Three more shapes that look like the failures are legal: a call to a function that returns a reference, a subscript, and a dereferenced pointer each name a real object, so assigning through them works.
One boundary is worth stating at the start. This error is about scalar assignment, where the left side must name the object being overwritten. For class types, assignment is a member function call, and a call can be made on a temporary object - so the same shape written with a class type can compile, its result discarded with the temporary. The five families here are all scalar, where no member function is involved and the rule applies directly.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs and the traps that compile are compiled and run, so their output is shown.
The rule underneath: the left side of an assignment must name an object
Expressions in C++ are classified by value category, and the category that matters here is the split between expressions that determine the identity of an object and expressions that only compute a value. An lvalue is an expression of the first kind: a variable name, a subscript, a dereferenced pointer, a call whose return type is an lvalue reference. A prvalue is an expression of the second kind: it computes a value or initializes an object, but the expression itself does not name a lasting place. For the built-in assignment operator, the rule is stated directly in the assignment operators reference: the target expression must be a modifiable lvalue - an expression that names an object, and one that is not read-only.
That single rule produces all five families, because five common expression forms are prvalues. A literal such as 5 is a prvalue: it is the value, with no object behind it. A function call whose return type is not a reference is a prvalue: the returned value is a copy, delivered to the caller and owned by no variable. An arithmetic expression such as a + b is a prvalue: it computes the sum, and the sum lives nowhere once computed. A cast to a non-reference type is a prvalue: static_cast<int>(a) produces the converted value, a copy of what a holds, not a itself. And a postfix increment is a prvalue: a++ evaluates to the value a had before the increment, a copy, while the increment itself happens to a as a side effect.
Three consequences explain every family below. First, parentheses do not change anything: parentheses preserve an expression's value category, so wrapping a prvalue as (a + b) keeps it a prvalue - the error in the third family is the sum, not the parentheses around it. Second, the right side is never the problem: any value can stand on the right, because the right side is only read; the repair always names or creates an object on the left. Third, "modifiable" is a separate requirement from "lvalue", and the traps show it: a const variable is an lvalue - it names an object - but it is not modifiable, and the compiler gives that case its own error rather than this one.
How to read the diagnostic
Every failure in the five families prints the same shape of output. The first family's, in full:
fail_literal.cpp:4:5: error: lvalue required as left operand of assignment
4 | 5 = 3;
| ^
Read it in order. The first line gives the file and the position - here the literal on the left of the = - and then the error itself: an lvalue was required as the left operand, and what stands there is not one. The marker on the third line sits under the left operand: in some families it is a single caret under the whole operand, in others a span of tildes with the caret inside it, but in every family it covers the expression on the left of the assignment, because that expression is the whole subject of the error.
Three practical consequences follow. First, read the left side, not the right: when this error appears, ask of the expression before the = only one question - what object does it name? If the answer is none, the error is explained. Second, the wording is exact about which operation failed: it says "left operand of assignment", and the neighboring operations have their own versions - the increment trap below says "lvalue required as increment operand" for the same underlying shape, so the operation named in the error is the operation to repair. Third, each family below produces exactly one error, because each program has exactly one bad assignment - a real program often has the same mistaken shape in several places, and each is reported separately, each fixed the same way.
The first failure: assigning to a literal
A program that writes an assignment with a literal as its target:
#include <iostream>
int main() {
5 = 3;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_literal.o fail_literal.cpp
fail_literal.cpp: In function ‘int main()’:
fail_literal.cpp:4:5: error: lvalue required as left operand of assignment
4 | 5 = 3;
| ^
The caret sits under the 5. A literal is the purest prvalue: 5 is a value written into the source, and there is no object it names - nothing in memory answers to it, so there is nowhere for the 3 to be stored. The shape is rare as a deliberate line of code; it appears when an assignment and a comparison are confused during editing, when a computed target is replaced by the number it happened to produce, or when the variable meant to receive the value is deleted and its last value is left standing in its place.
The fix names the object the value belongs in:
#include <iostream>
int main() {
int value = 0;
value = 3;
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_literal fix_literal.cpp
$ ./fix_literal
3
The output is 3. A variable was declared to hold the value, and the assignment writes into it. Every other family in this guide is this one with a less obvious value standing where the literal stands here - in each case the repair is the same shape: an object on the left.
The second failure: assigning to a function that returns by value
A program that assigns to the result of a function returning int:
#include <iostream>
int f() {
return 1;
}
int main() {
f() = 3;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_funcret.o fail_funcret.cpp
fail_funcret.cpp: In function ‘int main()’:
fail_funcret.cpp:8:6: error: lvalue required as left operand of assignment
8 | f() = 3;
| ~^~
The marker spans the call. A function that returns by value hands its caller a copy: the 1 that f returns is a prvalue, a value in transit, owned by no variable. Assigning to the call would mean writing into that copy - which is discarded as soon as the statement ends - so the language forbids it and the compiler reports the call as a non-lvalue target. The shape usually comes from code shaped like the accessor patterns of other languages, where a call can stand in for the storage it reaches into; in C++ that reach requires the function to return a reference, which is the first valid trap below.
The fix stores the returned value in a variable first, then assigns to the variable:
#include <iostream>
int f() {
return 1;
}
int main() {
int value = f();
value = 3;
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_funcret fix_funcret.cpp
$ ./fix_funcret
3
The output is 3. The returned copy now has a home, and the assignment writes into the variable that holds it. If the intent was instead for the assignment to change something inside the function's own state, the function's return type is the thing to change - return a reference to that state, and the call itself becomes an lvalue, as the traps show.
The third failure: assigning to a computed sum
A program that assigns to the sum of two variables:
#include <iostream>
int main() {
int a = 1;
int b = 2;
(a + b) = 5;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_sum.o fail_sum.cpp
fail_sum.cpp: In function ‘int main()’:
fail_sum.cpp:6:8: error: lvalue required as left operand of assignment
6 | (a + b) = 5;
| ~~~^~~~
The marker spans the whole parenthesized sum. The addition reads a and b and computes a third value; that result is a prvalue, and the parentheses around it change nothing, because parentheses preserve the category of the expression inside them. There is no object that is "the sum" - a and b are objects, and the sum is a value derived from them, with no storage the 5 could be written into. The shape appears when a line that computed a total is edited as if the total were a variable, or when an equation is transcribed directly into code: mathematically a + b = 5 states a relationship, but as C++ it is an instruction to store into a computation.
The fix gives the total a variable of its own:
#include <iostream>
int main() {
int a = 1;
int b = 2;
int total = a + b;
total = 5;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_sum fix_sum.cpp
$ ./fix_sum
5
The output is 5. The sum is computed into total, and the assignment then writes into total. If the intent was to make the sum come out to 5 by changing an input, the assignment belongs on the input instead - a = 5 - b; assigns to a, an object, and reaches the same total by a legal route.
The fourth failure: assigning to a cast result
A program that assigns to a variable through a cast:
#include <iostream>
int main() {
int a = 1;
static_cast<int>(a) = 5;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_cast.o fail_cast.cpp
fail_cast.cpp: In function ‘int main()’:
fail_cast.cpp:5:5: error: lvalue required as left operand of assignment
5 | static_cast<int>(a) = 5;
| ^~~~~~~~~~~~~~~~~~~
The caret underlines the whole cast. A cast to a non-reference type produces a converted value: static_cast<int>(a) reads a, converts the value it holds, and yields that value as a prvalue - a copy, even when the conversion is from int to int and changes nothing numerically. The copy is what stands on the left of the assignment, and a copy owned by no variable cannot be assigned to; the original a is untouched by the expression, on either side of the =. The shape appears when a cast added to the right side of an expression migrates to the left, or when a cast is written around a target in the belief that it converts the variable itself - a cast never converts a variable, it converts a value read from one.
The fix drops the cast and assigns to the variable directly:
#include <iostream>
int main() {
int a = 1;
a = 5;
std::cout << a << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_cast fix_cast.cpp
$ ./fix_cast
5
The output is 5. Where a conversion genuinely has to happen before the store, it belongs on the right side - a = static_cast<int>(some_value); converts the incoming value and stores it in a. The one cast form that can stand on the left is a cast to an lvalue reference type, such as static_cast<int&>(a), because its result names the object rather than copying its value - the same distinction the function trap draws between returning by value and returning an lvalue reference.
The fifth failure: assigning to a postfix increment result
A program that assigns to the result of a postfix increment:
#include <iostream>
int main() {
int a = 1;
a++ = 5;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_postinc.o fail_postinc.cpp
fail_postinc.cpp: In function ‘int main()’:
fail_postinc.cpp:5:6: error: lvalue required as left operand of assignment
5 | a++ = 5;
| ~^~
The marker spans a++. Postfix increment is defined by the copy it returns: a++ increments a, and the expression's value is what a held before the increment - a copy, delivered as a prvalue. The increment itself lands on a real object, but the expression as a whole names the copy, and the copy is the assignment's target here. The contrast inside the same operator family is exact: prefix increment, ++a, increments and then names a itself, so it is an lvalue and ++a = 5; compiles and assigns - the postfix form's extra copy is the whole difference.
The fix assigns to the variable the increment was applied to:
#include <iostream>
int main() {
int a = 1;
a = 5;
std::cout << a << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_postinc fix_postinc.cpp
$ ./fix_postinc
5
The output is 5. If both effects were intended - increment, then overwrite - they are two statements: a++; then a = 5;. An assignment combined with a postfix increment in one expression is asking the copy to be the storage, and the copy never is.
The fixes in reach order
- Ask what object the left side names. If it names none, the error is explained - name or create the object before fixing anything else.
- A literal on the left: declare the variable the value belongs in, and assign to the variable.
- A call on the left whose function returns by value: store the result in a variable and assign to that - or, if the assignment was meant to reach the function's own state, change the function to return a reference to it.
- A computation on the left - a sum, a product, any arithmetic result: compute it into a named variable and assign to the variable, or assign to one of the inputs instead.
- A cast on the left: assign to the variable under the cast, and move any needed conversion to the right side. Only a cast to an lvalue reference type names an object.
- A postfix increment or decrement on the left: assign to the variable itself; the postfix expression is the copy of its old value.
- A left side that names an object but still fails: check whether the error is a different one - a read-only target and an array target each have their own wording, in the traps.
Traps
- A
constvariable is an lvalue, but it is not modifiable - and its error is a different one. Here a constant is assigned to after its declaration:
#include <iostream>
int main() {
const int x = 1;
x = 3;
std::cout << x << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_const.o trap_const.cpp
trap_const.cpp: In function ‘int main()’:
trap_const.cpp:5:7: error: assignment of read-only variable ‘x’
5 | x = 3;
| ~~^~~
x names an object - and fails the second: the object is read-only, so it is not a modifiable lvalue. The two errors divide the rule between them: no object at all is "lvalue required"; an object that cannot be written is "read-only". The fix here is not a different target but a different declaration - drop the const if the variable is meant to change, or set its final value in the declaration if it is not.
- An array name cannot be assigned to at all - and its error is also a different one. Here one array is assigned to another:#include <iostream>
int main() {
int a[3] = {1, 2, 3};
int b[3] = {4, 5, 6};
a = b;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_array.o trap_array.cpp
trap_array.cpp: In function ‘int main()’:
trap_array.cpp:6:7: error: invalid array assignment
6 | a = b;
| ~~^~~
The wording is "invalid array assignment". An array is not a single assignable value: the language provides no assignment that replaces an array's contents wholesale, so the operation itself does not exist, rather than existing and lacking a target. Copying one array's elements into another is done element by element, or by holding the arrays in a type that is assignable as a whole, such as std::array or std::vector - whose elements can then be assigned individually, as the subscript trap below does.
- A value-only expression under ++ reports the increment, not the assignment. Here a computed sum is incremented:
#include <iostream>
int main() {
int a = 1;
(a + 1)++;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_inc.o trap_inc.cpp
trap_inc.cpp: In function ‘int main()’:
trap_inc.cpp:5:8: error: lvalue required as increment operand
5 | (a + 1)++;
| ~~~^~~~
The wording is "lvalue required as increment operand" - the same underlying shape as this guide's error, a prvalue standing where an object is required, reported against the operation that required it. Increment needs an object to increment, and (a + 1) names none. Reading the operation named in the error keeps the two apart: an assignment problem is fixed by naming a target for a store; an increment problem is fixed by naming the object to increment - here, a itself, as a++; or (a + 1) recomputed and stored with a = a + 1;.
- A function that returns an lvalue reference can be assigned through - its call is an lvalue. Here a function returns a reference to a variable, and the call is assigned to:
#include <iostream>
int stored = 0;
int& slot() {
return stored;
}
int main() {
slot() = 7;
std::cout << stored << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_ref_return trap_ref_return.cpp
$ ./trap_ref_return
7
The output is 7. The return type is the whole difference from the second family: returning int& means the call names the object the reference refers to - here stored - so the call is an lvalue and the assignment writes through it into stored. A call is not inherently a bad target; a call returning by value is, because only the copy comes back.
- A subscript names an element, so it can be assigned to. Here the first element of a vector is overwritten:
#include <iostream>
#include <vector>
int main() {
std::vector<int> values = {1, 2, 3};
values[0] = 9;
std::cout << values[0] << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_subscript trap_subscript.cpp
$ ./trap_subscript
9
The output is 9. values[0] is not a computed value in the third family's sense: the subscript operator reaches into the container and names its first element, an object with storage, so the expression is an lvalue. An expression built from operators can stand on the left whenever the operator's result is the object itself rather than a value derived from it - the subscript is the common case, and the next trap is the other one.
- A dereferenced pointer names the object it points at, so it can be assigned through. Here a value is written through a pointer:
#include <iostream>
int main() {
int value = 0;
int* ptr = &value;
*ptr = 4;
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_deref trap_deref.cpp
$ ./trap_deref
4
The output is 4. *ptr follows the pointer to the object it points at and names that object, so the assignment writes into value - which is what the printed value confirms. Dereference, subscript, and a reference-returning call are the three everyday expression forms that name objects through something else; each is an lvalue for the same reason a variable name is.
Checklist
- The left side of every assignment read as a question: what object does this expression name? A value with no object behind it is the error.
- A literal never a target - the value it holds stored in a declared variable instead.
- A function call as a target checked for its return type: by value is a copy and fails; an lvalue reference return names an object and works.
- An arithmetic result never a target - computed into a named variable, or one of its inputs assigned instead.
- A cast as a target checked for its target type: a cast to a value type copies and fails; conversions moved to the right side; only a cast to an lvalue reference type names the object.
- Postfix
++and--results treated as copies of the old value, never as targets - the variable itself assigned or incremented. - A failure on a target that does name an object matched against the neighboring errors before assuming this one: read-only for a
consttarget, invalid array assignment for an array, increment operand when the operation is++. - Subscripts, dereferenced pointers, and reference-returning calls left alone as targets - each names an object.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Cannot Bind Rvalue Reference Fix
- C++ Read-Only Assignment Fix
- Fix 'Cannot Bind Non-const Lvalue Reference to an Rvalue' in C++ — Temporaries, Function Arguments, and const&
- 'std::unique' Is Not a Member of 'std' Fix
Sources
- cppreference, Value categories - for lvalues naming objects and prvalues only computing values, for literals, calls returning non-reference types, arithmetic expressions, postfix increment results, and casts to non-reference types being prvalues, and for calls returning lvalue references, prefix increment, dereference, and subscript expressions being lvalues.
- cppreference, Assignment operators - for the built-in simple assignment requiring its target expression to be a modifiable lvalue, and for the result of an assignment being an lvalue referring to its target.
- cppreference, Expressions - for every expression having both a type and a value category, and for parentheses preserving an expression's value, type, and value category.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.