0Pricing
C++ Academy · Lesson

consteval and constinit

Modern compile-time keywords.

Beyond constexpr

C++20 adds two keywords that sharpen compile-time programming: consteval for guaranteed compile-time functions and constinit for guaranteed compile-time initialization.

consteval: Immediate Functions

A consteval function must be evaluated at compile time. Unlike constexpr, it can never run at runtime.

consteval int cube(int x) {
    return x * x * x;
}

int main() {
    constexpr int c = cube(3);   // OK: compile time
    return c;
}

All lessons in this course

  1. const Variables and Parameters
  2. const Member Functions
  3. constexpr and Compile-Time
  4. consteval and constinit
← Back to C++ Academy