Mastering C++ Const Correctness and Mutability. Triggers: const, mutable, logically const, bitwise const, data race, mutex, shared_mutex, thread safety.
Who is allowed to change this state?
C++ defaults to Mutable. You must opt-in to safety with const.
const methods promise not to change visible state.mutable to change hidden state (like caches) inside const methods.| Issue | Design Question |
| ------------------------- | ----------------------------------------------------------------------- |
| Discarded qualifiers | Are you trying to modify member data in a const function? |
| Data Race | Did you modify a mutable field from multiple threads without locking? |
| Iterator Invalidation | Are you modifying a container while reading it? |
Should this be const?
const or constexpr.const T* (ptr to const) vs T* const (const ptr).Is internal state 'invariant' or 'cache'?
mutable + std::mutex.Is it thread-safe?
const methods implies safe for concurrent readers.mutable members MUST be protected by synchronization in const methods.Trace Up:
const method modified a mutable cache without a lock, assuming const meant safe.std::mutex to the class, lock it in the const method.Trace Down:
std::shared_mutex allowing multiple readers (shared_lock) and one writer (unique_lock).| Keyword | Meaning | Use When |
| ----------------------- | --------------------- | -------------------------------- |
| const | Read-only access | Parameters, local vars, getters. |
| constexpr | Compile-time constant | Constants, array sizes. |
| mutable | Modifiable in const | Caches, Mutexes within a class. |
| std::mutex | Exclusive Lock | Protecting mutable state. |
| std::shared_mutex | Read/Write Lock | Rare updates, frequent reads. |
Search for places (restaurants, cafes, etc.) via Google Places API proxy on localhost.
Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries.
Create or update AgentSkills. Use when designing, structuring, or packaging skills with scripts, references, and assets.
Start voice calls via the OpenClaw voice-call plugin.
Notion API for creating and managing pages, databases, and blocks.
Gemini CLI for one-shot Q&A, summaries, and generation.
Category:developer