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:
- The function
squareis declared asconsteval, ensuring it can only be evaluated at compile-time. - The variable
resultis initialized usingsquare(5), which is evaluated during compilation. - Attempting to call
squareat runtime (e.g., using a non-constexprvariable) 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:
- The
factorialfunction is declared asconsteval, ensuring it can only be evaluated at compile-time. - The
constexprvariablefact_5is initialized usingfactorial(5), which computes the factorial of 5 during compilation. - Attempting to call
factorialat runtime would result in a compilation error.
Key Points to Remember about consteval Keyword
- The
constevalkeyword enforces compile-time evaluation of functions. - A
constevalfunction cannot be called at runtime; it must always be evaluated during compilation. constevalis useful for generating constant values, performing static checks, and ensuring immutability.- Attempting to use a
constevalfunction in a non-constant context results in a compilation error. - It is a C++20 feature, so a C++20-compatible compiler must be used with the
-std=c++20flag enabled.
