Created
October 6, 2016 01:04
-
-
Save tsuna/61be0fd5069e85d6fc16eb652423b6dd to your computer and use it in GitHub Desktop.
factorial in meta-programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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