const prevents modification after initialization. constexpr requests compile-time evaluation when arguments are constant—enabling zero-cost constants and static checks.
Examples
const int maxUsers = 100;
constexpr int square(int x) { return x * x; }
constexpr int area = square(10);
const member functions promise not to modify observable object state (except mutable members).
Important interview questions and answers
- Q: const vs constexpr?
A:constis runtime immutability;constexprimplies const and enables compile-time use when evaluable. - Q: const T& parameter?
A: Read-only access without copying—idiomatic for large inputs.
Self-check
- Can a constexpr function run at runtime too?
- What does const on a member function promise?
Tip: Mark member functions const when they do not modify observable state—enables use on const objects.
Interview prep
- const vs constexpr?
constprevents modification;constexprenables compile-time evaluation when arguments are constant.- const member function?
Promises not to modify non-mutable observable state—callable on const objects.