consteval and constinit
consteval marks a function that must run at compile time; a runtime call is a hard error. constinit marks a static-duration variable that must be initialised before the program starts, without making the variable itself const.
Detailed Explanation
C++20 split two jobs that constexpr was being asked to do at once. Sometimes you want a function that is never allowed to run when the program is live. Sometimes you want a global that is ready before main without the "static initialisation order fiasco", but that you can still write to later. Those are consteval and constinit.
A consteval function is an immediate function. Every call must be a constant expression. The compiler evaluates it while compiling, and the call is replaced by the result. If any argument is not known at compile time, the program does not compile. That is stricter than constexpr, which means "can run at compile time when the arguments allow it, otherwise it is a normal function".
Reach for consteval when a runtime call would be a bug. A function that turns a string literal into a packed four-character code, a function that validates a format string, a function that builds a lookup table from a list of enumerators: if someone later calls those with a runtime std::string, you want the build to fail, not a surprise branch in production. consteval is the type system saying "this is a compile-time tool".
constinit is about when a variable is initialised, not about whether you can assign to it. A variable with static storage duration (static locals, globals, namespace-scope objects, static members) is supposed to be initialised before any code reads it. The language already initialises some of them at compile time (constant initialisation). The rest run as dynamic initialisation, in a poorly specified order across translation units. Two globals in different .cpp files that refer to each other can see a zeroed object that has not run its constructor yet. That is the static initialisation order fiasco.
constinit forces constant initialisation. The initialiser must be a constant expression. The object is ready before any dynamic initialiser runs. The object is not const: you can assign to it later. That combination is what constexpr cannot give you, because a constexpr object is immutable.
A typical use is a process-wide cache or a configuration object you fill during startup but want constructed in a known, pre-main state: constinit std::array<int, 8> slots{};. Another is a static local you do not want lazily initialised on first call: constinit is only for variables that already have static storage duration, and a static local with constinit is initialised at compile time rather than on first pass.
consteval and constexpr compose. A constexpr function may call a consteval function only when that call itself is a constant expression. A consteval function may call constexpr functions freely, because those can run at compile time.
constinit does not replace constexpr for values you use as array bounds or template arguments. A constinit int n = 4; is not a constant expression when you read it, because it is allowed to change. Use constexpr for those reads.
Code Examples
#include <cstdint>
#include <iostream>
consteval std::uint32_t fourcc(const char s[5]) {
// Must be a string literal of length 4 plus the null.
return (static_cast<std::uint32_t>(s[0]) << 24)
| (static_cast<std::uint32_t>(s[1]) << 16)
| (static_cast<std::uint32_t>(s[2]) << 8)
| static_cast<std::uint32_t>(s[3]);
}
int main() {
constexpr auto tag = fourcc("PNG ");
std::cout << std::hex << tag << '\n'; // prints: 504e4720
// const char* p = "PNG ";
// auto bad = fourcc(p); // error: not a constant expression
}#include <array>
#include <iostream>
// Ready before main. Not const: startup code may fill it.
constinit std::array<int, 3> ports{8080, 8081, 8082};
int main() {
ports[0] = 9090; // allowed: constinit is not const
std::cout << ports[0] << '\n'; // prints: 9090
}Common Gotchas
A `consteval` call with a runtime argument does not fall back
there is no generated runtime body. If you need both modes, write `constexpr`.
`constinit` is not a constant expression
`std::array<int, ports[0]>` will not compile even if you never assign to `ports`. Use `constexpr` when the value must be used at compile time.
`static constinit` locals skip the hidden guard
an ordinary `static T x = ...;` is initialised on first entry and needs a thread-safe guard. `constinit` initialises it before `main`, so the first call has no lazy constructor cost.
Immediate functions cannot take runtime pointers to string data
even if the characters look like a literal, a `const char*` parameter that did not originate as a constant array is not a constant expression.