Advertisement
Intermediate C++, Constexpr & Compile-Time

What is consteval (C++20)?

Short 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.

Save C++ mock interview
Advertisement
AModel 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.
Likely follow-ups
  • How does consteval differ from constexpr? | What happens on a non-constant call?
Related topics