Skip to content

Instantly share code, notes, and snippets.

@mirekfranc
Created July 3, 2015 02:11
Show Gist options
  • Save mirekfranc/05b190ef69847b81947f to your computer and use it in GitHub Desktop.
Save mirekfranc/05b190ef69847b81947f to your computer and use it in GitHub Desktop.
factorial with C++ templates
#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