C++
C++ Read-Only Assignment Fix
When you assign to a variable, the variable has to be modifiable. If it was declared const - directly, through auto, as a loop variable, with constexpr, or as the type of a value a lambda captured - compilation stops with 'assignment of read-only variable'. The name in the error is the variable on the left of the assignment, and the const is in that variable's declaration, which may be some distance from the line that fails. This guide separates the cases, reads what the diagnostic actually reports, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 8 min read
- The rule underneath: a const object cannot be modified, and assignment needs a modifiable lvalue
- How to read the diagnostic
- The first failure: a const local variable reassigned later
- The second failure: a const auto variable updated in a loop
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
In x = value, x has to be a modifiable variable. If x was declared const, compilation stops with "assignment of read-only variable 'x'". The variable named in the error is the one on the left of the assignment, and the reason is in its declaration, not in the value on the right.
The causes in this guide each reproduce the failure with a small, complete program: a const local variable reassigned later, a const auto variable updated inside a loop, a const range-for loop variable, a constexpr variable, and a const variable captured by value in a lambda. In each family the failing program is small, the compiler output is shown, and the fix removes the const, moves the final value into the declaration, uses a separate working variable, or changes how the value is captured, so the assignment is to a modifiable variable.
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 are compiled and run, so their output is shown.
This guide is about a variable that is itself const. It is a different diagnostic from "discards qualifiers", which is about calling a non-const operation through a const-qualified access path, and from "lvalue required as left operand of assignment", which is about the left side not being an assignable variable at all - a literal or a temporary, for example. Here the left side is a real variable; it is the variable's own const that blocks the assignment.
The rule underneath: a const object cannot be modified, and assignment needs a modifiable lvalue
A const object is an object whose type is const-qualified, or a non-mutable subobject of a const object. Such an object cannot be modified: an attempt to do so directly is a compile-time error. That is the whole rule behind this diagnostic - const is not a hint or a style marker, it is part of the variable's type, and it stays in force for the variable's whole lifetime.
Assignment is the operation that runs into it. For the built-in assignment operators, the target expression must be a modifiable lvalue, and the object it refers to is modified by replacing its value. A const variable is not modifiable, so it fails that requirement before anything else about the assignment is considered - the type and value on the right do not matter.
Three consequences of that rule explain the families below. First, initialization is not assignment: const int limit = 10; is fine, because the declaration gives the object its one value, and only a later limit = 20; fails. Second, const can be present without the word appearing next to the failing line - auto can carry it from the declaration, a range-for declares its loop variable inside the loop header, and a lambda capture copies the captured variable's type. Third, compound assignment and increment are assignments for this rule: count += 3 on a const int count reports this same error, and ++step on a const int step reports the close wording "increment of read-only variable".
That gives a fast way to read any instance of this error: take the variable name from the diagnostic, find its declaration, and look for the const - written directly, carried by auto or a typedef, implied by constexpr, or copied in by a capture. The fix is then a decision about that declaration: either the variable was meant to change, in which case it should not be const, or it was meant to stay fixed, in which case the code that assigns to it should work on a different variable or compute the final value before the declaration.
How to read the diagnostic
Every failure in the main families prints the same shape of output. The first family's, in full:
fail_local.cpp: In function ‘int main()’:
fail_local.cpp:4:11: error: assignment of read-only variable ‘limit’
4 | limit = 20;
| ~~~~~~^~~~
Read it in order. The first line gives the file and the function. The error line names the line and column of the assignment, and the caret sits under the whole assignment expression, starting at the variable. The name in the message is the variable on the left - that is the declaration to go and read. There is no "did you mean" and no note pointing at the declaration: GCC reports the assignment site, and finding the const in the declaration is your job. The last word of the message does classification work too - "variable" here, but "parameter", "location", "reference", and "member" in the neighboring shapes at the end of this guide, each with a different fix.
The first failure: a const local variable reassigned later
A program that declares a limit, then tries to raise it:
#include <iostream>
int main() {
const int limit = 10;
limit = 20;
std::cout << limit << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_local.o fail_local.cpp
fail_local.cpp: In function ‘int main()’:
fail_local.cpp:4:11: error: assignment of read-only variable ‘limit’
4 | limit = 20;
| ~~~~~~^~~~
The declaration on line 3 is legal - a const variable must be initialized, and it is. Line 4 is a second value for the same object, and that is what const forbids. Nothing about the right side matters: limit = 10;, assigning the value it already holds, fails in exactly the same way.
The fix depends on intent. If the variable is meant to change, it is not a constant, and the const comes off:
#include <iostream>
int main() {
int limit = 10;
limit = 20;
std::cout << limit << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_local fix_local.cpp
$ ./fix_local
20
The output is 20. If the variable is meant to stay fixed, keep the const and delete the assignment instead - or, when the later value is the one the program actually needs, put that value in the declaration: const int limit = 20; with no second assignment. What does not work is keeping both the const and the reassignment and changing something else; the declaration and the assignment contradict each other, and one of them has to go.
The second failure: a const auto variable updated in a loop
A program that accumulates a total that was declared with const auto:
#include <iostream>
int main() {
const auto total = 5;
int sum = 0;
for (int i = 0; i < 3; ++i) {
total = total + i;
sum += i;
}
std::cout << total << " " << sum << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_auto.o fail_auto.cpp
fail_auto.cpp: In function ‘int main()’:
fail_auto.cpp:6:15: error: assignment of read-only variable ‘total’
6 | total = total + i;
| ~~~~~~^~~~~~~~~~~
auto deduces the type from the initializer, and the const written with it is part of that type: total is a const int. The word auto can make the declaration look lighter than it is, but the qualifier is fully in force, and the first pass through the loop already fails - an accumulator is by definition a variable that changes, so it cannot be const, however it was declared. The same shape appears when the const arrives through a typedef: with typedef const int CInt;, a variable declared CInt value = 7; reports "assignment of read-only variable 'value'" when it is reassigned, and the const is visible only in the typedef.
The fix drops the const from the working variable:
#include <iostream>
int main() {
auto total = 5;
int sum = 0;
for (int i = 0; i < 3; ++i) {
total = total + i;
sum += i;
}
std::cout << total << " " << sum << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_auto fix_auto.cpp
$ ./fix_auto
8 3
The output is 8 3: total runs 5, 5, 6, 8 across the three passes, and sum is 3. If only part of the computation is fixed, split it: keep the starting value in a const variable (const auto start = 5;) and accumulate into a separate non-const variable initialized from it. That split is usually the right shape when the const was added to protect the starting value rather than the accumulator.
The third failure: a const range-for loop variable
A program that tries to adjust each element's copy inside a range-for:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
for (const int x : v) {
x = x + 10;
std::cout << x << "\n";
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_range.o fail_range.cpp
fail_range.cpp: In function ‘int main()’:
fail_range.cpp:6:11: error: assignment of read-only variable ‘x’
6 | x = x + 10;
| ~~^~~~~~~~
The loop variable is a declaration, and const int x declares a constant one. A range-for variable declared by value is already a copy of each element in turn, so the const is not protecting the vector - it is only preventing the copy from being adjusted inside the body. That is a common reason this family appears: const is added to a loop variable out of habit, in code that then treats the variable as a working value.
The fix makes the loop variable non-const:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
for (int x : v) {
x = x + 10;
std::cout << x << "\n";
}
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_range fix_range.cpp
$ ./fix_range
11
12
13
The output is 11, 12, 13, one per line. The vector itself is unchanged, because x is a copy - if the intent was to change the elements in place, the loop variable needs to be a reference, for (int& x : v), and then assignments to x write through to the vector. Choose based on which thing the adjusted value belongs to: the copy for local use, or the element for a lasting change.
The fourth failure: a constexpr variable
A program that tries to resize something declared constexpr:
#include <iostream>
int main() {
constexpr int size = 4;
size = 8;
std::cout << size << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_constexpr.o fail_constexpr.cpp
fail_constexpr.cpp: In function ‘int main()’:
fail_constexpr.cpp:4:10: error: assignment of read-only variable ‘size’
4 | size = 8;
| ~~~~~^~~
constexpr implies const for a variable: a constexpr variable is a compile-time constant, not a variable that merely starts out known at compile time. The word const never appears in this program, which is why this family is worth separating - searching the declaration for const finds nothing, and the qualifier is carried by constexpr itself. A value that is computed at run time, read from input, or changed by program logic cannot be constexpr, and a value that is constexpr cannot be assigned to after its declaration.
The fix, when the value does need to change, is a plain variable:
#include <iostream>
int main() {
int size = 4;
size = 8;
std::cout << size << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_constexpr fix_constexpr.cpp
$ ./fix_constexpr
8
The output is 8. If the value genuinely is a compile-time constant, keep constexpr and remove the assignment - compute the one final value and put it in the declaration. The middle option, when both a fixed base size and a working size exist, is two variables: the constexpr base, and a non-const working variable initialized from it.
The fifth failure: a const variable captured by value in a lambda
A program that captures a const variable by copy and tries to change the copy:
#include <iostream>
int main() {
const int base = 100;
auto add = [base]() mutable {
base = 200;
return base;
};
std::cout << add() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_capture.o fail_capture.cpp
fail_capture.cpp: In lambda function:
fail_capture.cpp:5:14: error: assignment of read-only variable ‘base’
5 | base = 200;
| ~~~~~^~~~~
The capture copies base, and the copy has base's type - const int. The mutable keyword on the lambda does not rescue it: mutable makes the lambda's call operator non-const, which is what allows captured copies to be modified at all, but it does not remove a const that is part of a captured variable's own type. So this program already has the keyword people reach for first with lambda capture errors, and it still fails, with the diagnostic naming the captured variable rather than anything about the lambda.
The fix depends on which value is meant to change. Here the intent is to change the original, so the capture becomes a reference capture of a non-const variable:
#include <iostream>
int main() {
int base = 100;
auto add = [&base]() {
base = 200;
return base;
};
std::cout << add() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_capture fix_capture.cpp
$ ./fix_capture
200
The output is 200. If the intent was a private working copy inside the lambda, keep the capture by value but drop the const from the original variable's declaration, and keep mutable on the lambda - both are needed, because the copy's type comes from the declaration and permission to modify the copy comes from mutable. If the captured value is meant to stay fixed, the assignment is the bug: read base inside the lambda and store the result in a local variable instead.
The fixes in reach order
- The variable is declared
constand the code needs it to change: remove theconstfrom its declaration. - The variable is meant to stay fixed and the assignment computes a later state: keep the
const, and put the changing state in a separate non-const working variable initialized from it - or move the final value into the declaration and delete the assignment. - The
constarrived throughautoor a typedef: read the full declared type, not just the line that fails - the qualifier is in the declaration or the alias. - The variable is a range-
forloop variable: drop theconstfor a working copy, or declare it as a reference (int&) when the elements themselves are meant to change. - The variable is
constexpr: it isconstby implication - either it keeps one compile-time value, or it becomes a plain variable. - The variable is a lambda capture: check the captured variable's declared type and the capture kind -
mutablealone does not remove a capturedconst. - The wording is different - "read-only parameter", "read-only location", "read-only reference", "read-only member", "in read-only object": that is a neighboring problem, not this one - see the traps.
Traps
- A
constfunction parameter reports "read-only parameter", not "read-only variable". The repair is the same in spirit - drop theconstif the parameter is meant to be a working variable, which is safe because a by-value parameter is already a copy - but the wording tells you the declaration to look at is in the function's parameter list:
#include <iostream>
void setLimit(const int limit) {
limit = 20;
std::cout << limit << "\n";
}
int main() {
setLimit(10);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_param.o trap_param.cpp
trap_param.cpp: In function ‘void setLimit(int)’:
trap_param.cpp:3:11: error: assignment of read-only parameter ‘limit’
3 | limit = 20;
| ~~~~~~^~~~
setLimit(int), without the const - top-level const on a by-value parameter does not change the function's type, only whether the parameter can be assigned to inside the body.
- Assigning through a pointer to const reports "read-only location", because the variable named in the code is the pointer, and the thing being assigned to is the location it points at:#include <iostream>
int main() {
int value = 10;
const int* p = &value;
*p = 20;
std::cout << value << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_ptr.o trap_ptr.cpp
trap_ptr.cpp: In function ‘int main()’:
trap_ptr.cpp:5:8: error: assignment of read-only location ‘* p’
5 | *p = 20;
| ~~~^~~~
value itself is not const here - the access path is. The fix is at the pointer: use an int* if this code is meant to modify through it, or assign to value directly. The same "read-only location" wording appears for an element of a const array, reported as 'values[0]' for const int values[3].
- Assigning through a reference to const reports "read-only reference":
#include <iostream>
void bump(const int& value) {
value = value + 1;
std::cout << value << "\n";
}
int main() {
int x = 10;
bump(x);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_ref.o trap_ref.cpp
trap_ref.cpp: In function ‘void bump(const int&)’:
trap_ref.cpp:3:11: error: assignment of read-only reference ‘value’
3 | value = value + 1;
| ~~~~~~^~~~~~~~~~~
The caller's variable x is not const; the reference parameter is a read-only view of it. A function that is meant to change the caller's variable takes a non-const reference, int& - changing the parameter type is the fix, and it also changes what the function promises its callers.
- A const data member assigned outside its constructor reports "read-only member", not "read-only variable":
#include <iostream>
class Limit {
public:
Limit(int value) : value_(value) {}
void set(int value) { value_ = value; }
int value() const { return value_; }
private:
const int value_;
};
int main() {
Limit limit(10);
limit.set(20);
std::cout << limit.value() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_member_int.o fail_member_int.cpp
fail_member_int.cpp: In member function ‘void Limit::set(int)’:
fail_member_int.cpp:5:34: error: assignment of read-only member ‘Limit::value_’
5 | void set(int value) { value_ = value; }
| ~~~~~~~^~~~~~~
A const member gets its one value in the member-initializer list, as this class's constructor does - there is no setter for a const member, by design. If the member is meant to be settable, remove its const; if it is meant to be fixed at construction, delete the setter and pass the final value to the constructor. A const member of class type fails differently again: assigning to a const std::string member reports "no match for 'operator='" rather than any read-only wording, because the assignment is rejected inside overload resolution for the member's own assignment operator.
- Assigning to a member of a const object reports the object as read-only, even when the member itself is not const:
#include <iostream>
class Point {
public:
int x = 0;
};
int main() {
const Point p;
p.x = 5;
std::cout << p.x << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_member_constobj.o trap_member_constobj.cpp
trap_member_constobj.cpp: In function ‘int main()’:
trap_member_constobj.cpp:8:9: error: assignment of member ‘Point::x’ in read-only object
8 | p.x = 5;
| ~~~~^~~
Here the declaration to change is the object's: Point p; without the const, with x given its value at construction or in a later assignment. The same "in read-only object" wording appears when a non-const member is assigned to from inside a const member function - there, it is the member function's own const that makes the object read-only inside its body.
- Increment is not assignment in the message. ++step on a const int step reports "increment of read-only variable 'step'", with the caret under ++step. The cause and the fix are the same as the first family's - the variable is const and the code needs it to change - but searching for the assignment wording will not find it, so read for "read-only" rather than for the full phrase.
Checklist
- The variable named in the diagnostic has been found at its declaration, and its full type - including any
constcarried byauto, a typedef,constexpr, or a capture - is known. - The intent is settled: the variable either changes (no
const) or it does not (no assignment to it) - not both. - A fixed starting value that feeds changing state lives in a
constvariable, with the changing state in a separate non-const working variable. - A range-
forvariable that is adjusted in the body is non-const, and is a reference if the elements themselves are meant to change. - A
constmember is given its value in the member-initializer list, and has no setter. - A lambda that modifies a captured copy captures a non-const variable by value and is marked
mutable; a lambda that modifies the original captures by reference. - A "parameter", "location", "reference", "member", or "in read-only object" wording is fixed at the parameter, pointer, reference, member, or object declaration it names - not by editing the variable in this guide's families.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Cannot Declare Variable of Abstract Type Fix
- C++ Lvalue Required Assignment Fix
- How Fast Do std::latch and std::barrier Wake Threads? Measured — 9 to 68 Times Faster Than a Condition Variable
- C++20 Coroutines + io_uring: A co_await File Read That Actually Runs
Sources
- cppreference, cv (const and volatile) type qualifiers - for a const object being an object whose type is const-qualified, and such an object not being modifiable, with a direct attempt being a compile-time error.
- cppreference, Assignment operators - for assignment operators modifying the value of the object, for the built-in simple assignment requiring the target expression to be a modifiable lvalue, and for compound assignment behaving as the corresponding simple assignment with the target evaluated once.
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.