Why is const correctness important in API design?
Short answer
const correctness makes interfaces self-documenting (which parameters and methods modify state), lets const objects be used freely, enables the compiler to catch accidental mutations, supports thread-safety reasoning (const often implies read-only), and is required to…
Advertisement
AModel answer
const correctness makes interfaces self-documenting (which parameters and methods modify state), lets const objects be used freely, enables the compiler to catch accidental mutations, supports thread-safety reasoning (const often implies read-only), and is required to interoperate cleanly with const objects and references. Retrofitting const later is hard, so it's designed in from the start.
Real-world example
A well-const-qualified API lets callers pass const objects and reason about which calls are read-only.
Likely follow-ups
- How does const aid thread-safety reasoning? | Why design const in from the start?
Related topics