Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Keywords and Statements

The consteval and constinit specifiers

Reference for C++20 consteval and constinit: immediate functions that must run at compile time, constant initialization of statics, and if consteval.

C++20 added two specifiers next to constexpr that close gaps it left. consteval declares an immediate function: every call must produce a compile-time constant, otherwise the program is ill-formed. A plain constexpr function only may run at compile time.

constinit applies to variables with static or thread storage duration. It requires that the variable is constant-initialized, so the static initialization order problem cannot affect it, but the variable itself remains modifiable at runtime (it is not const).

C++
consteval int cube(int n) { return n * n * n; }
constinit int counter = cube(3);   // initialized at compile time, mutable later

Try it

C++ consteval: calls must be constants

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++ constinit and if consteval

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.

Syntax

C++
consteval return-type function(params) { body }
constinit static-or-thread-variable = constant-initializer;
if consteval { /* constant evaluation only */ } else { /* runtime */ }   // C++23
Specifier Applies to Requirement
consteval Functions, constructors Every call (direct or through a pointer) must evaluate to a constant at compile time. Implicitly inline.
constinit Variables with static or thread storage The initializer must be a constant expression. The variable need not be const.
constexpr Variables, functions For comparison: values are constants; functions may run at either time.

Usage notes

  • A consteval function cannot be called with a runtime value, even from another constexpr function, unless that call itself is in an immediate context (for example, in another consteval function, or since C++23 inside if consteval).
  • Destructors and allocation functions cannot be consteval. A function cannot be both constexpr and consteval.
  • Use consteval to guarantee zero runtime cost, for checks such as validating a literal at compile time, computing lookup tables, or building format-string checkers. std::format checks its format string this way.
  • constinit fixes the static initialization order fiasco for a variable: since it is initialized at compile time, no other global's constructor can see it uninitialized.
  • constinit on a thread_local variable also avoids the hidden guard check on each access, since no dynamic initialization is needed.
  • constinit does not imply const or constexpr; combine constinit const when you need both guarantees.
  • if consteval (C++23) is the safer replacement for if (std::is_constant_evaluated()), which has a trap: inside if constexpr it is always true.
  • Compilers: GCC 10+, Clang 10+ and MSVC 19.29+ support C++20 consteval/constinit; if consteval needs GCC 12+.