Constexpr

An example that shows how to use constexpr for compile-time evaluation.

cppCopy code#include <iostream>

constexpr int factorial(int n) {
return n &lt;= 1 ? 1 : n * factorial(n - 1);
} int main() {
std::cout &lt;&lt; "Factorial of 5: " &lt;&lt; factorial(5) &lt;&lt; std::endl; // Output: 120
return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *