C++ Reference ยท Types and Language Concepts
Compile-time programming in C++
Concept page on C++ compile-time evaluation: constexpr, consteval, constinit, if consteval, static_assert and what a constant expression may do.
C++ can run ordinary code while the program is being compiled. The compiler evaluates constant expressions whenever the language needs a value at compile time (array bounds, template arguments, static_assert, constexpr variables) and may do so as an optimization elsewhere. The keywords constexpr, consteval and constinit say how strictly you want that to happen.
Compile-time code is regular C++: loops, branches, local variables, std::array, and since C++20 even std::vector and std::string whose memory is released before the evaluation ends. Anything that would be undefined behavior makes the expression non-constant, so the compiler tells you about mistakes at build time.
constexpr int square(int n) { return n * n; } static_assert(square(12) == 144);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The keywords
constexprvariable: must be initialized by a constant expression and is implicitlyconst. Usable wherever a constant is required.constexprfunction: may be evaluated at compile time. With constant arguments in a constant context it is; otherwise it runs like any function. Before C++23 aconstexprfunction that could never produce a constant was ill-formed (no diagnostic required); C++23 dropped that rule.constevalfunction (C++20): an immediate function. Every call must produce a constant, otherwise the program is ill-formed. Use it for things that must never cost run time, such as computing tables or validating format strings.constinit(C++20): applied to a variable with static or thread storage. It guarantees constant initialization (no static initialization order problems) but the variable stays mutable.if constexpr: a compile-timeifin templates. The discarded branch is not instantiated. Seeifandelse.if consteval(C++23): picks a branch depending on whether the function is currently being constant-evaluated. Braces are required.static_assert: checks a constant condition during compilation. Seestatic_assert.
What may run at compile time
The set has grown with each standard:
- C++11: a single
returnexpression. - C++14: loops, local variables, multiple statements.
- C++17:
constexprlambdas,if constexpr. - C++20: virtual functions,
tryblocks (without throwing), dynamic allocation that is freed within the evaluation (sostd::vector/std::stringwork),std::is_constant_evaluated(),consteval,constinit. - C++23:
if consteval,static constexprvariables insideconstexprfunctions, non-literal local variables and labels allowed inconstexprfunctions as long as the evaluation does not reach them, andstd::unique_ptrusable in constant expressions.
Still forbidden in a constant expression: goto into a scope, reinterpret_cast, calling a non-constexpr function, reading uninitialized or out-of-lifetime objects, signed overflow, out-of-range array access, throw that is actually executed, and I/O.
Usage notes
constexprdoes not force compile-time evaluation of a call. To guarantee it, assign the result to aconstexprvariable, use it in a template argument, or call aconstevalfunction.- Failure is a compile error, not a crash. A
static_assert(f(3) == 9)that fails, orconstexpr int x = 1 / 0;, stops the build with a diagnostic pointing at the offending operation. - Compile time costs compile time. Heavy loops at compile time slow the build; compilers cap the number of steps (
-fconstexpr-ops-limiton GCC). - Prefer functions over template metaprogramming for computing values; the code is easier to read and debug.
- Header-defined
constexprfunctions are implicitlyinline, so they do not violate the One Definition Rule. - Classes can be literal types: a class with a
constexprconstructor can be created at compile time, which is howstd::array,std::string_viewandstd::optionalwork in constant expressions.