consteval and constinit
Modern compile-time keywords.
consteval and constinit is a free C++ Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “consteval and constinit” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “consteval and constinit”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
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
- const Variables and Parameters
- const Member Functions
- constexpr and Compile-Time
- consteval and constinit