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
- const Variables and Parameters
- const Member Functions
- constexpr and Compile-Time
- consteval and constinit