Home C++ Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----

C++ 14


Contents

Constexpr

C++14 has relaxed the rules for what we can put inside the body of a function including but not limited to "if" and "loop" statements.

File: constexpr1.cpp
#include <iostream>

using namespace std ;

constexpr int factorial(int n)
{
  if (n <= 1)
  {
    return 1;
  }
  else
  {
    return n * factorial(n - 1) ;
  }
}


int main()
{
     constexpr int val = factorial(5)  ;
    cout << val << endl ;

 return 1 ;
}