Constexpr in C++
Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value of a constexpr function must be known at compile time and cannot be changed at runtime.
One of the main benefits of using constexpr is that it enables the compiler to optimize the code simultaneously. This can lead to improved performance and reduced memory usage at runtime.
To declare a variable as a constexpr, add the keyword constexpr
before the type in the declaration. For example:
constexpr int x = 10;
In this case, x
is a compile-time constant with the value 10. You can also initialize a constexpr variable with the result of an expression, as long as the expression can be evaluated at compile time:
constexpr int y = x * 2; // y is equal to 20
You can also declare constexpr functions, which return a compile-time constant. Add the constexpr keyword before the return type in the function declaration to do this. For example:
constexpr int square(int x) {
return x * x;
}
In this case, the square
function returns the square of its input x
. Because the return value of this function can be determined at compile time, it can be used in other constexpr declarations and expressions:
constexpr int y = square(10); // y is equal to 100
Note that constexpr functions can also have multiple statements and loops as long as the return value can be determined at compile time. However, not all functions can be declared as constexpr – for example, a function that generates a random number cannot be a constexpr function because the return value is not known at compile time.
Examples:
#include <iostream>
// Define a constexpr function to compute the square of a number
constexpr int square(int x) {
return x * x;
}
int main() {
// Compute the square of a number at compile time
constexpr int a = square(5);
// Print the result to the console
std::cout << "The square of 5 is " << a << std::endl;
return 0;
}
Output:
Explanation:
In this code, the square function is declared as constexpr, which allows the call to square(5) in the main function to be computed at compile time. This can be useful for optimizing code and ensuring that certain values are known at compile time.
Here is another example of how constexpr
can be used in C++:
#include <iostream>
// Define a constexpr function that returns the factorial of a number
constexpr int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
int main() {
// Compute the factorial of a number at compile time
constexpr int a = factorial(5);
// Print the result to the console
std::cout << "The factorial of 5 is " << a << std::endl;
return 0;
}
Output:
Explanation:
In this example, the factorial
function is declared constexpr, allowing the main function's call to factorial(5) to be computed at compile time. This allows the value of the factorial to be known at compile time rather than having to be calculated at runtime.
Here is another example program that demonstrates the use of constexpr
in C++:
#include <iostream>
// Define a constexpr function that calculates the sum of two numbers
constexpr int add(int a, int b)
{
return a + b;
}
int main()
{
// Use the constexpr function to calculate the sum of 5 and 7 at compile time
constexpr int result = add(5, 7);
// Print the result
std::cout << "The sum of 5 and 7 is " << result << std::endl;
return 0;
}
Output:
Explanation:
This program defines a constexpr
function called add
that calculates the sum of two numbers. The constexpr
keyword indicates that the function can be evaluated at compile time if the arguments passed are constant expressions. In the main
function, we use the constexpr
function to calculate the sum of 5 and 7 and print the result.
Since the arguments passed to the add
function are constant expressions (the literal values 5
and 7
), the function is evaluated at compile time, and the result is stored in the result
variable. This means that the add
function does not need to be executed at runtime, which can improve the program's performance.