Back to slide -- Keyboard shortcut: 'u'        next -- Keyboard shortcut: 'n'          constexpr/constexpr-1.cpp - Examples of constant expressions - C++11.Lecture 6 - slide 33 : 40
Program 1

// A number of examples of constant expressions (C++11).

#include <iostream>

constexpr int I1 = 5, I2 = 7, I3 = 9;
constexpr int I4 = (I1 % 2 == 0) ? I2 : I3;     

constexpr bool even(int n){
  return n % 2 == 0;
}
constexpr int I5 = even(I1) ? I2 : I3;           // Eqvivalent to I4

constexpr long int fac(int n){
 return (n == 0) ? 1 : n * fac(n - 1);
}
constexpr long int FAC5 = fac(5);
constexpr long int FAC10 = fac(10);

constexpr long int fib(int n){
  return (n == 0) ? 0 : (n == 1) ? 1 : fib(n - 1) + fib(n - 2);
} 
constexpr long int FIB20 = fib(20);

// int i3 = 3;
// constexpr int I6 = (I1 % 2 == 0) ? I2 : i3;   // Error: i3 is not a const, and not a constexpr

int main(){                                      // In order to check the results from about (at run time).                        
  using namespace std;    
   
  cout << I1 << " " << I2 << " " << I3 << endl;  // 5 7 9
  cout << I4 << endl;                            // 9
  cout << I5 << endl;                            // 9
  cout << FAC5  << endl;                         // 120
  cout << FAC10 << endl;                         // 362800

  int i = 10;
  cout << fac(i) << endl;                        // 362800 - fac can also be called at run time.

  cout << FIB20 << endl;                         // 6765
}