constexpr
Tells the compiler “compute this at compile time, if at all possible.”
constexpr int square(int x) { return x * x; }
int arr[square(4)]; // 16 - computed at compile time, valid array size
constexpr int val = square(5); // 25, baked into the binary, zero runtime cost
vs #define
#define MAX_HEALTH 100 // dumb text substitution, no type, no scope
constexpr int MAX_HEALTH = 100; // an actual typed constant, respects scope,
// shows up properly in a debugger
constexpr gets everything a macro constant gives, plus type safety, without any of the macro pitfalls.
Doesn’t force compile-time — it allows it
int x = getUserInput();
int y = square(x); // still works fine, just computed at runtime instead
A constexpr function can still be called with a value only known at runtime — it just falls back to running normally, like any other function.