Skip to content

Instantly share code, notes, and snippets.

@tsuna
Created October 6, 2016 01:04
Show Gist options
  • Save tsuna/61be0fd5069e85d6fc16eb652423b6dd to your computer and use it in GitHub Desktop.
Save tsuna/61be0fd5069e85d6fc16eb652423b6dd to your computer and use it in GitHub Desktop.
factorial in meta-programming
#include <iostream>
template <int i>
struct factorial {
enum {
result = factorial<i-1>::result * i,
};
};
template <>
struct factorial<1> {
enum {
result = 1,
};
};
template <>
struct factorial<0> {
enum {
result = 1,
};
};
int main() {
std::cout << factorial<5>::result << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment