Skip to content

Instantly share code, notes, and snippets.

@kjk
Created May 31, 2020 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjk/ff346afecaaadfe112531619f21eadeb to your computer and use it in GitHub Desktop.
Save kjk/ff346afecaaadfe112531619f21eadeb to your computer and use it in GitHub Desktop.
#include <iostream>
template<unsigned int n>
struct factorial
{
enum
{
value = n * factorial<n - 1>::value
};
};
template<>
struct factorial<0>
{
enum { value = 1 };
};
int main()
{
std::cout << factorial<7>::value << std::endl; // prints "5040"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment