An example that shows how to use constexpr
for compile-time evaluation.
cppCopy code#include <iostream>
constexpr int factorial(int n) {
return n <= 1 ? 1 : n * factorial(n - 1);
}
int main() {
std::cout << "Factorial of 5: " << factorial(5) << std::endl; // Output: 120
return 0;
}
Leave a Reply