0Pricing
C++ Academy · Lesson

consteval and constinit

Modern compile-time keywords.

consteval and constinit is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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;
}

consteval Rejects Runtime Calls

Calling a consteval function with a runtime value is a compile error — the guarantee is enforced.

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

int main() {
    int n = 4;
    // int bad = cube(n);   // ERROR: n is not a constant
    return cube(2);
}

Why consteval?

Use it when a computation makes no sense at runtime — for example, generating a constant table or validating a literal at compile time.

constexpr vs consteval

constexpr functions may run at compile time; consteval functions always do. Choose consteval to forbid accidental runtime use.

constinit: Guaranteed Static Init

constinit guarantees a static or thread-local variable is initialized at compile time, eliminating the static initialization order fiasco.

constinit int globalCount = 10;   // initialized at compile time

int main() {
    return globalCount;
}

constinit Is Not const

Unlike constexpr, a constinit variable can still be modified after its compile-time initialization — it only constrains how it is initialized.

constinit int counter = 0;

int main() {
    counter = 5;   // OK: constinit allows mutation
    return counter;
}

Solving the Init-Order Fiasco

For non-local statics across translation units, constinit forces static (compile-time) initialization, so the value is ready before any dynamic init runs.

Three Keywords Compared

constexpr: may be compile time, is const. consteval: must be compile time (functions). constinit: forces compile-time initialization, stays mutable.

Compiler Support

These are C++20 features. Compile with -std=c++20 (or later) on GCC/Clang, or /std:c++20 on MSVC.

Choosing the Right Tool

Reach for consteval to force compile-time computation, and constinit to make static initialization deterministic — together they make compile-time C++ safer.

Quick Check

Test your understanding of consteval and constinit.

Recap

You learned the C++20 keywords consteval (functions that must run at compile time) and constinit (guaranteed compile-time initialization for statics, still mutable). Together with constexpr they give precise control over compile-time evaluation.

Frequently asked questions

Is the “consteval and constinit” lesson free?

Yes — the full text of “consteval and constinit” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “consteval and constinit”?

Modern compile-time keywords. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “consteval and constinit” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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