C++ › Const Correctness

What is the difference between physical and logical constness?

Advanced

Answer

Physical (bitwise) constness means no bits of the object change. Logical constness means the object's observable/meaningful state doesn't change, even if some internal bits (like a cache or mutex) do. const member functions guarantee logical constness; mutable members let internal, non-observable state change while preserving logical constness.
// A const method that lazily caches a result maintains
// logical constness while mutating a mutable cache member.
Real-world example A lazily-computed, cached hash preserves logical constness though the cache member physically changes.

Common follow-ups: Which does const guarantee? | How does mutable relate to this?