CKTIS / C++
Compile-time evaluation
Compile-time evaluation determines values under C++ constant-expression rules. Learn how constexpr, consteval, and static_assert differ, and distinguish branch selection with if constexpr from conditional compilation controlled by a feature flag.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
constexpr int square(int x) { return x * x; }
consteval int immediate_square(int x) { return x * x; }
constexpr int value = square(6);
static_assert(value == 36);
if constexpr (constant_condition) { /* ... */ }
#if FEATURE_ENABLED
// conditionally included code
#endifCompile-time evaluation (compile time evaluation) determines values under constant-expression rules rather than requiring their computation while the program runs. A constexpr variable must be constant-initialized: the requirement applies to the full initialization, not to the initializer as written. For example, constexpr int a[] = {1, 2}; is valid in C++23 even though the braced initializer {1, 2} is not an expression. static_assert checks a condition during compilation. This is not merely optimization: a forbidden operation in a required constant expression causes a compilation error.
A constexpr function can participate in constant evaluation, but an ordinary call to it need not be evaluated during compilation. This depends on its arguments and context. consteval declares an immediate function: an immediate invocation of it must be a constant expression. const alone does not guarantee that a value is usable in a constant expression.
A feature flag (feature flag) is a configuration switch, not a C++ keyword. In the example, the FEATURE_ENABLED macro controls conditional compilation (conditional compilation) through #if, #else, and #endif. The preprocessor skips the unselected group. #if operates on macros, not constexpr variables; #ifdef tests only whether a macro exists, so it also accepts a macro defined as 0.
if constexpr selects a branch using a constant condition, but does not remove text like the preprocessor. In a template, the discarded branch is not instantiated once the condition is no longer dependent on template parameters; outside templates, it is still checked for validity. The example requires constant evaluation of result, then prints it at runtime. This entry targets C++23: the source is an evolving draft, not a frozen C++23 publication. A repository compiler flag does not attest the production runtime environment.
Examples
#include <iostream>
#ifndef FEATURE_ENABLED
#define FEATURE_ENABLED 1
#endif
constexpr int square(int x) {
return x * x;
}
#if FEATURE_ENABLED
constexpr int result = square(6);
static_assert(result == 36);
#else
constexpr int result = square(3);
static_assert(result == 9);
#endif
int main() {
std::cout << result << '\n';
}
Result
36
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.