CKTIS / C++
constexpr — constants and functions for constant evaluation
constexpr requires a constant initializer for a variable and allows a function to participate in constant expressions. It does not force every function call to be evaluated during compilation.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
constexpr Type name = constant_expression;
constexpr ReturnType function_name(parameters) { /* body */ }For an object, constexpr implies const, requires a literal type, and requires an initializer satisfying constant-expression rules. const alone does not guarantee constant initialization. Thus, constexpr int area = square(6); requires valid constant evaluation.
A constexpr function can participate in a constant expression when that particular call satisfies the relevant rules. It can also accept data available only during execution. static_assert requires a constant expression; an ordinary call imposes no such requirement. Signed integer overflow or calling a non-constexpr function on the evaluated path prevents constant evaluation.
Do not declare parameters as constexpr. consteval is for immediate functions, whereas constinit requires static initialization of a variable with static or thread storage duration without automatically making it const.
The example requires constant evaluation of square(6) and checks the result with static_assert. The call square(side) does not require constant evaluation; the optimizer may still simplify it. This entry targets C++23. The linked document is an evolving draft, not a frozen C++23 edition.
Examples
#include <iostream>
constexpr int square(int x) {
return x * x;
}
int main() {
constexpr int area = square(6);
static_assert(area == 36);
int side = 7;
std::cout << area << ' ' << square(side) << '\n';
}
Result
36 49
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.