Created
July 3, 2015 02:11
-
-
Save mirekfranc/05b190ef69847b81947f to your computer and use it in GitHub Desktop.
factorial with C++ templates
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<unsigned long M> | |
struct f | |
{ | |
static const unsigned long value = M * f<M-1>::value; | |
}; | |
template<> | |
struct f<0> | |
{ | |
static const unsigned long value = 1UL; | |
}; | |
int | |
main () | |
{ | |
std::cout << f<0>::value << std::endl; | |
std::cout << f<3>::value << std::endl; | |
char a[f<10>::value]; | |
std::cout << sizeof (a) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment