Why isn't constexpr guaranteed to run during compilation?
Additionally, why was consteval
added instead of changing constexpr
to guarantee a compile-time execution?
constexpr
already guarantees compile-time evaluation when used on a variable.
If used on a function it is not supposed to enforce compile-time evaluation since you want most functions to be usable at both compile-time and runtime.
consteval
allows forcing functions to not be usable at runtime. But that is not all that common of a requirement.
Here a modified answer, since technically constexpr
and consteval
are about whether or not certain evaluations are constant expressions or must be constant expressions. This does not mean that the compiler must evaluate these expressions at compile-time (as there couldn't possibly be any observable difference), however that's the intent:
constexpr
already guarantees that the full-expression of initialization is a constant expression when used on a variable.
If used on a function it is not supposed to enforce evaluation in a constant expression at the call site since you want most functions to be usable both inside and outside constant expressions.
consteval
allows forcing functions to be callable only as constant expressions. But that is not all that common of a requirement.