Created
March 3, 2018 22:27
-
-
Save zzag/82d2191227bb5d31c95e645eff577480 to your computer and use it in GitHub Desktop.
template madness: factorial
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> | |
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