C++ › Constexpr & Compile-Time

What is consteval (C++20)?

Intermediate

Answer

consteval declares an 'immediate function' that must be evaluated at compile time—every call must produce a constant, otherwise it's a compile error. Unlike constexpr (which may run at compile or run time), consteval guarantees compile-time evaluation, useful for functions that must never incur runtime cost or that generate compile-time-only values.
consteval int cube(int x) { return x*x*x; }
constexpr int c = cube(3);  // OK
// int r = cube(runtimeVar); // error: not constant
Real-world example A consteval function guarantees a hash or ID is computed at compile time, never at runtime.

Common follow-ups: How does consteval differ from constexpr? | What happens on a non-constant call?