agentleFS
13eholder13eholder/Modern-Cpp-Skills/m03-mutability/SKILL.md

m03-mutability

Skill14 starsChanged 9 months agoMITView on GitHub

Mastering C++ Const Correctness and Mutability. Triggers: const, mutable, logically const, bitwise const, data race, mutex, shared_mutex, thread safety.

7 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Files
SKILL.md

C++ Mutability & Const Correctness

Core Question

Who is allowed to change this state?

C++ defaults to Mutable. You must opt-in to safety with const.

  • API Contract: const methods promise not to change visible state.
  • Physical Constness: The compiler ensures bits don't change.
  • Logical Constness: You use mutable to change hidden state (like caches) inside const methods.

Error → Design Question

IssueDesign Question
Discarded qualifiersAre you trying to modify member data in a const function?
Data RaceDid you modify a mutable field from multiple threads without locking?
Iterator InvalidationAre you modifying a container while reading it?

Thinking Prompt

  1. Should this be const?
    • Variables: Yes, default to const or constexpr.
    • Methods: Yes, unless it must modify state.
    • Pointers: const T* (ptr to const) vs T* const (const ptr).
  2. Is internal state 'invariant' or 'cache'?
    • Cache/Memoization? → Use mutable + std::mutex.
    • Actual state? → Non-const method.
  3. Is it thread-safe?
    • const methods implies safe for concurrent readers.
    • mutable members MUST be protected by synchronization in const methods.

Trace Up / Down

  • Trace Up:
    • Issue: "Undefined behavior in multithreaded app."
    • Cause: A const method modified a mutable cache without a lock, assuming const meant safe.
    • Fix: Add std::mutex to the class, lock it in the const method.
  • Trace Down:
    • Intent: "I want a thread-safe lookup table."
    • Code: std::shared_mutex allowing multiple readers (shared_lock) and one writer (unique_lock).

Quick Reference

KeywordMeaningUse When
constRead-only accessParameters, local vars, getters.
constexprCompile-time constantConstants, array sizes.
mutableModifiable in constCaches, Mutexes within a class.
std::mutexExclusive LockProtecting mutable state.
std::shared_mutexRead/Write LockRare updates, frequent reads.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.