What discarding qualifiers means
The C++ diagnostic passing 'const …' as 'this' argument discards qualifiers means a const object is calling a member function that is not marked const. Every non-static member function receives an implicit object argument named this. A non-const member function receives a pointer to a modifiable object. A const object cannot be passed to that function without removing its const promise, so the compiler rejects the call.
Read the function name and the source expression together. The object may be declared const directly, or the program may reach it through a const reference or pointer. The member declaration determines which calls are allowed; the spelling of the call does not change that rule.
error: passing 'const Counter' as 'this' argument discards qualifiers [-fpermissive]
A const object cannot call a mutating member
This counter's get function reads its field but does not promise const behavior. Calling it through a const object therefore fails even though the current body only returns a value. The compiler checks the function's declared contract, not whether this particular execution appears to write state.
struct Counter {
int value = 6;
int get() { return value; }
};
int main() {
const Counter counter;
return counter.get();
}
MSVC reports error C2662: 'int Counter::get(void)': cannot convert 'this' pointer from 'const Counter' to 'Counter &'. Mark a read-only member function const after its parameter list. That qualifier changes the implicit object parameter so the function can be called through const objects and const references.
#include <iostream>
struct Counter {
int value = 6;
int get() const { return value; }
};
int main() {
const Counter counter;
std::cout << counter.get() << '\n';
}
6
A const reference preserves the same restriction
A function that accepts const Counter& promises not to modify the counter through that parameter. Inside the function, the expression counter is const-qualified even when the caller passed a non-const object. Calling a non-const get through that reference violates the parameter's contract and produces C2662.
#include <iostream>
struct Counter {
int value = 7;
int get() { return value; }
};
void show(const Counter& counter) {
counter.get();
}
int main() {
Counter counter;
show(counter);
std::cout << counter.get() << '\n';
}
MSVC reports error C2662: 'int Counter::get(void)': cannot convert 'this' pointer from 'const Counter' to 'Counter &' at counter.get(). The fix is still to declare this read-only operation as int get() const; changing the parameter to a mutable reference would weaken the API instead of expressing what the getter does.
#include <iostream>
struct Counter {
int value = 7;
int get() const { return value; }
};
void show(const Counter& counter) {
counter.get();
}
int main() {
Counter counter;
show(counter);
std::cout << counter.get() << '\n';
}
7
Put the const qualifier on read-only member functions
For an ordinary member function, the trailing const qualifies the implicit object argument. It promises that the function does not modify the object's observable state through this. A getter usually meets that promise. A function that changes a field should remain non-const, and callers need a non-const object to invoke it.
Const qualification is part of a member function's type and overload set. You can provide both const and non-const overloads when their behavior differs, but avoid duplicate overloads when both can share one const implementation. Member functions declared static have no this pointer, so this rule does not apply to them.
A const member can read ordinary data members and call other const members through this. It cannot call a non-const member through that same object, even if the called function currently appears to read only. A separate non-const object passed as an argument has its own qualification and can still be changed when the function contract allows it. Keep each const qualifier attached to the object expression it describes.
A set element is const, and a map key is const
Elements reached through a std::set iterator are const because changing an element in place could break the ordering that keeps the set searchable. A std::map key is const, but its mapped value remains mutable. Calling std::string::clear() through a set element or a map key produces MSVC C2662. Copy the string into a mutable variable before editing it. In C++17, extract() also lets you change a map key through a node handle.
#include <set>
#include <string>
int main() {
std::set<std::string> names{"Ada"};
names.begin()->clear();
}
error: passing 'const std::__cxx11::basic_string<char>' as 'this' argument discards qualifiers [-fpermissive]MSVC reports error C2662: 'void std::basic_string<char,std::char_traits<char>,std::allocator<char>>::clear(void) noexcept': cannot convert 'this' pointer from 'const std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &' because the set element is const. Copy the string into a mutable variable before clearing it.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> names{"Ada"};
std::string name = *names.begin();
name.clear();
std::cout << name.size() << '\n';
}
0
These container rules are separate from the member function declaration, but they lead to the same call-site restriction: the object expression is const. Check whether constness comes from the type's member declaration, a function parameter, or a standard container before choosing a fix.
Exercise
Make the read operation available through a const Box object. The program should print:
9
struct Box { int value = 9; int read() { return value; } };
int main() { const Box box; return box.read(); }
error: passing 'const Box' as 'this' argument discards qualifiers [-fpermissive]Show one possible solution
#include <iostream>
struct Box { int value = 9; int read() const { return value; } };
int main() { const Box box; std::cout << box.read() << '\n'; }
Related guides
- Understand assignment targets
- Fix function argument mismatches
- Find syntax errors
- Search compiler diagnostics
Keep learning with CppPath
Continue with the full lesson const Correctness. It includes a quiz and checked coding challenge available with a free account.