constexpr/constexpr-4.cpp - Evaluation of constexpr at run-time - C++11. | Lecture 6 - slide 33 : 40 Program 3 |
// Illustrates that constexpr functions can be evaluated at run-time as well. #include <iostream> constexpr long int fib(int n){ return (n == 0) ? 0 : (n == 1) ? 1 : fib(n - 1) + fib(n - 2); } constexpr long int fac(int n){ return (n == 0) ? 1 : n * fac(n - 1); } int main(){ using namespace std; int i; cout << "Enter an integer: "; cin >> i; cout << "Fib(i): " << fib(i) << " fac(i): " << fac(i) << endl; }