## calculation of factorial using different facilities of C++ language

I’ve collected all known techniques to calculate factorial using C++. Enjoy

1. using free function

int f(int n)

{

return n <= 1 ? 1 : n * f(n – 1);

}

int x = f(42);

2. using lambdas

std::function f = [&f](int n) -> int

{

return n <= 1 ? 1 : n * f(n – 1);

};

int x = f(42);

3. using constexpr

constexpr int f(int n)

{

return n <= 1 ? 1 : n * f(n – 1);

}

const int x = f(42);

4. using templates

template

struct factorial

{

enum { value = N * factorial::val; };

};

template

struct factorial

{

enum { value = 0; }

};

const int x = Factorial::value;

## Reply