Skip to content

Instantly share code, notes, and snippets.

@undetected1
Created September 8, 2011 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undetected1/1202995 to your computer and use it in GitHub Desktop.
Save undetected1/1202995 to your computer and use it in GitHub Desktop.
Template metaprogramming
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
int y = Factorial<0>::value; // == 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment