Consteval

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 constevalmust 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 the consteval function is evaluated at compile-time without any issues.
  • If we try to pass a runtime value (runtime_value) to the square function, it will result in a compilation error because consteval functions cannot be called at runtime.

Key Characteristics of consteval

  1. 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.
  2. Function Behavior:
    • Like constexpr, a consteval 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.
  3. 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.
  4. 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.

Difference Between consteval and constexpr

The differences between consteval and constexpr can be summarized as follows:

Featureconstevalconstexpr
Evaluation TimeMust always be evaluated at compile-time.Can be evaluated at compile-time or runtime depending on how it is called.
Allowed CallsCan only be called with constant expressions.Can be called with constant expressions (compile-time) or runtime values.
Runtime CallsCompilation error if called with runtime values.Can be evaluated at runtime if called with runtime values.
Function ComplexityCan 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 ExecutionAlways ensures compile-time evaluation.Only guarantees compile-time evaluation when called with constant expressions.
Use CaseUsed 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.