Introduced in C++20, the consteval
keyword is a new addition that complements the constexpr
keyword, allowing you to enforce compile-time evaluation of a function. Unlike constexpr
, which may be evaluated at compile-time or runtime depending on how the function is called, a function declared as consteval
must always be evaluated at compile-time.
What is consteval
?
consteval
stands for constant evaluation, and it enforces that a function can only be called at compile-time. Any attempt to call a consteval
function at runtime will result in a compilation error. This ensures that the value or result of the function is always computed during compilation, which can be particularly useful in certain scenarios where compile-time guarantees are critical.
Syntax:
consteval returnType functionName(parameters) {
// Function body
}
Example:
consteval int square(int x) {
return x * x;
}
int main() {
constexpr int result = square(5); // Compile-time evaluation, works fine
// int runtime_value = 10;
// int runtime_result = square(runtime_value); // Error: cannot call consteval function with runtime values
}
In this example:
square(5)
is a compile-time constant, so theconsteval
function is evaluated at compile-time without any issues.- If we try to pass a runtime value (
runtime_value
) to thesquare
function, it will result in a compilation error becauseconsteval
functions cannot be called at runtime.
Key Characteristics of consteval
- Mandatory Compile-time Execution:
- A
consteval
function must always be evaluated at compile-time. If you try to call it with runtime data or in a context where the result isn't known at compile-time, the compiler will generate an error.
- A
- Function Behavior:
- Like
constexpr
, aconsteval
function can contain complex logic, such as loops, branches, and local variables, but the key difference is that it must be evaluated at compile-time. - The function cannot be called with values that are only available at runtime.
- Like
- Guaranteed Performance:
- Since
consteval
functions are evaluated at compile-time, they have zero runtime cost. This can lead to more efficient code, especially for scenarios that benefit from compile-time computation, such as compile-time configuration or mathematical operations.
- Since
- Difference from
constexpr
:- While
constexpr
functions can be evaluated at compile-time (if called with constant expressions), they may also be evaluated at runtime (if called with runtime values). In contrast,consteval
functions must be evaluated at compile-time and cannot be called at runtime.
- While
Difference Between consteval
and constexpr
The differences between consteval
and constexpr
can be summarized as follows:
Feature | consteval | constexpr |
---|---|---|
Evaluation Time | Must always be evaluated at compile-time. | Can be evaluated at compile-time or runtime depending on how it is called. |
Allowed Calls | Can only be called with constant expressions. | Can be called with constant expressions (compile-time) or runtime values. |
Runtime Calls | Compilation error if called with runtime values. | Can be evaluated at runtime if called with runtime values. |
Function Complexity | Can include complex logic like loops and branches, but must be resolved at compile-time. | Can include complex logic, and may resolve either at compile-time or runtime. |
Guaranteed Compile-time Execution | Always ensures compile-time evaluation. | Only guarantees compile-time evaluation when called with constant expressions. |
Use Case | Used when you need to force compile-time evaluation, especially for validation or calculation. | Used when compile-time evaluation is preferred but not required for all calls. |