Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          constexpr/constexpr-5.cpp - Relaxed constexpr in C++14.Lecture 6 - slide 33 : 40
Program 4

// Constexpr as of C++14. Not yet compilable in g++. Due in gcc version 5.

#include <iostream>
constexpr long int fib(int n){
  int a = 0, b = 1, tmp;
  for(int i = 1; i <= n; ++i){
    tmp = a; a = b; b = tmp + b;
  }
  return a;
} 

constexpr long int fac(int n){
  int res = 1;
  for(int i = 1; i <= n; ++i)
    res = res * i;
  return res;
}

int main(){
  using namespace std;

  constexpr int FAC5 = fac(5);
  constexpr int FIB10 = fib(10);

  cout << "FAC5 " << FAC5 << "   FIB10: " << FIB10 << endl;
}