Skip to content
Tutorials Center
My learning

Topics

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.

C++
constexpr int square(int n) { return n * n; }
static_assert(square(12) == 144);

Try it

C++ constexpr, consteval and static_assert

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.

C++ if consteval and constinit

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

  • constexpr variable: must be initialized by a constant expression and is implicitly const. Usable wherever a constant is required.
  • constexpr function: may be evaluated at compile time. With constant arguments in a constant context it is; otherwise it runs like any function. Before C++23 a constexpr function that could never produce a constant was ill-formed (no diagnostic required); C++23 dropped that rule.
  • consteval function (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-time if in templates. The discarded branch is not instantiated. See if and else.
  • 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. See static_assert.

What may run at compile time

The set has grown with each standard:

  • C++11: a single return expression.
  • C++14: loops, local variables, multiple statements.
  • C++17: constexpr lambdas, if constexpr.
  • C++20: virtual functions, try blocks (without throwing), dynamic allocation that is freed within the evaluation (so std::vector/std::string work), std::is_constant_evaluated(), consteval, constinit.
  • C++23: if consteval, static constexpr variables inside constexpr functions, non-literal local variables and labels allowed in constexpr functions as long as the evaluation does not reach them, and std::unique_ptr usable 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

  • constexpr does not force compile-time evaluation of a call. To guarantee it, assign the result to a constexpr variable, use it in a template argument, or call a consteval function.
  • Failure is a compile error, not a crash. A static_assert(f(3) == 9) that fails, or constexpr 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-limit on GCC).
  • Prefer functions over template metaprogramming for computing values; the code is easier to read and debug.
  • Header-defined constexpr functions are implicitly inline, so they do not violate the One Definition Rule.
  • Classes can be literal types: a class with a constexpr constructor can be created at compile time, which is how std::array, std::string_view and std::optional work in constant expressions.