The qualifiers that control mutability and where it can be bent.
int x = 0;
const int max = 100; // a constant
const int* p1; // pointer to const int (can't change *p1)
int* const p2 = &x; // const pointer (can't repoint p2)
const int* const p3 = &x; // both
void print(const std::string& s); // const ref param: no copy, no mutation
struct Counter {
int value() const; // const member function: callable on const objects, won't mutate *this
};Read pointer declarations right-to-left: int* const = "const pointer to int".
Mark everything that doesn't mutate as const — parameters, member functions,
locals. It documents intent, enables optimizations, and lets your API be called
on const objects. A const member function can only call other const members
and can't modify data members (except mutable ones).
struct Result { int value; };
Result compute(); // the expensive call being cached
class Cache {
mutable std::mutex m_; // lockable even from const methods
mutable std::optional<Result> cached_;
public:
Result get() const { // logically const, physically caches
std::lock_guard lk{m_};
if (!cached_) cached_ = compute();
return *cached_;
}
};mutable says "this member isn't part of the object's logical state" — typically
caches, mutexes, lazy-computed values.
constexpr is "const and known at compile time"; it's stronger than const.
See constant expressions.
int read();
const int a = read(); // const, but value computed at runtime
constexpr int b = 5 * 5; // const AND a compile-time constantvolatile int* hardware_reg; // tells the compiler: don't optimize away reads/writesvolatile means "this memory can change outside the program's control" (memory-
mapped hardware, signal handlers). It does not provide atomicity or ordering —
use std::atomic for threads.
constmember functions andmutable: aconstmethod can modifymutablemembers; use that sparingly for caches/mutexes only — overuse hides real mutation.- Top-level vs low-level const.
const int* p(points to const) differs fromint* const p(const pointer).autodrops top-level const; spell it out if you want it kept. constexprimpliesconstfor variables, but is much stronger; don't use plainconstand expect compile-time evaluation.volatileis widely misused for concurrency — it's wrong for that. It's for hardware/signal memory only.- Don't
const_castaway const to mutate a truly-const object — UB. See conversions.