C++ consteval Keyword

The consteval keyword in C++ was introduced in C++20. It is used to declare functions that are guaranteed to be evaluated at compile-time. These functions are known as immediate functions. Any attempt to call a consteval function at runtime will result in a compilation error.

The consteval keyword is particularly useful when you want to enforce compile-time evaluation of certain operations, such as generating constant values or performing static checks.


Syntax

</>
Copy
consteval return_type function_name(parameters) {
    // Function body
}
consteval
The keyword that ensures the function is evaluated at compile-time.
return_type
The type of the value the function returns.
function_name
The name of the function.
parameters
The input parameters required by the function.

Examples

Example 1: Compile-Time Constant Function

This example demonstrates a consteval function that computes the square of a number at compile-time.

</>
Copy
#include <iostream>
using namespace std;

// Define a consteval function
consteval int square(int x) {
    return x * x;
}

int main() {
    constexpr int result = square(5); // Compile-time evaluation
    cout << "Square of 5: " << result << endl;

    // Uncommenting the following line will cause a compilation error
    // int runtime_result = square(5);

    return 0;
}

Output:

Square of 5: 25

Explanation:

  1. The function square is declared as consteval, ensuring it can only be evaluated at compile-time.
  2. The variable result is initialized using square(5), which is evaluated during compilation.
  3. Attempting to call square at runtime (e.g., using a non-constexpr variable) will result in a compilation error.

Example 2: Generating Compile-Time Constants

This example shows how to use a consteval function to generate compile-time constants.

</>
Copy
#include <iostream>
using namespace std;

// Define a consteval function
consteval int factorial(int n) {
    if (n < 0) throw "Factorial not defined for negative numbers";
    return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}

int main() {
    constexpr int fact_5 = factorial(5); // Compile-time evaluation
    cout << "Factorial of 5: " << fact_5 << endl;

    // Uncommenting the following line will cause a compilation error
    // int fact_runtime = factorial(5);

    return 0;
}

Output:

Factorial of 5: 120

Explanation:

  1. The factorial function is declared as consteval, ensuring it can only be evaluated at compile-time.
  2. The constexpr variable fact_5 is initialized using factorial(5), which computes the factorial of 5 during compilation.
  3. Attempting to call factorial at runtime would result in a compilation error.

Key Points to Remember about consteval Keyword

  1. The consteval keyword enforces compile-time evaluation of functions.
  2. A consteval function cannot be called at runtime; it must always be evaluated during compilation.
  3. consteval is useful for generating constant values, performing static checks, and ensuring immutability.
  4. Attempting to use a consteval function in a non-constant context results in a compilation error.
  5. It is a C++20 feature, so a C++20-compatible compiler must be used with the -std=c++20 flag enabled.