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).
consteval int cube(int n) { return n * n * n; } constinit int counter = cube(3); // initialized at compile time, mutable later
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.
Syntax
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
constevalfunction cannot be called with a runtime value, even from anotherconstexprfunction, unless that call itself is in an immediate context (for example, in anotherconstevalfunction, or since C++23 insideif consteval). - Destructors and allocation functions cannot be
consteval. A function cannot be bothconstexprandconsteval. - Use
constevalto guarantee zero runtime cost, for checks such as validating a literal at compile time, computing lookup tables, or building format-string checkers.std::formatchecks its format string this way. constinitfixes the static initialization order fiasco for a variable: since it is initialized at compile time, no other global's constructor can see it uninitialized.constiniton athread_localvariable also avoids the hidden guard check on each access, since no dynamic initialization is needed.constinitdoes not implyconstorconstexpr; combineconstinit constwhen you need both guarantees.if consteval(C++23) is the safer replacement forif (std::is_constant_evaluated()), which has a trap: insideif constexprit is alwaystrue.- Compilers: GCC 10+, Clang 10+ and MSVC 19.29+ support C++20
consteval/constinit;if constevalneeds GCC 12+.