Skip to content

Instantly share code, notes, and snippets.

@zzag
Created March 3, 2018 22:27
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 zzag/82d2191227bb5d31c95e645eff577480 to your computer and use it in GitHub Desktop.
Save zzag/82d2191227bb5d31c95e645eff577480 to your computer and use it in GitHub Desktop.
template madness: factorial
#include <iostream>
using std::cout;
using std::endl;
template <int N>
struct factorial {
constexpr static int value = N * factorial<N-1>::value;
};
template <>
struct factorial<0> {
constexpr static int value = 1;
};
int
main() {
std::cout << "5! = " << (factorial<5>::value) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment