Skip to content

Instantly share code, notes, and snippets.

@jb55
Created November 27, 2010 06:30
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 jb55/717637 to your computer and use it in GitHub Desktop.
Save jb55/717637 to your computer and use it in GitHub Desktop.
Static factorial in clay
procedure fact;
overload fact(static 0) = 1;
[n | n > 0]
overload fact(static n) = fact(static n - 1) * n;
main() {
var x = fact(static 4); // == 24, calculated at compile time
}
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
void foo() {
int x = Factorial<4>::value; // == 24
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment