Advertisement
Advanced C++, Const Correctness

What is the difference between physical and logical constness?

Short 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.

Save C++ mock interview
Advertisement
AModel 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.
Example

// 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.
Likely follow-ups
  • Which does const guarantee? | How does mutable relate to this?
Related topics