Created
November 27, 2010 06:30
-
-
Save jb55/717637 to your computer and use it in GitHub Desktop.
Static factorial in clay
This file contains hidden or 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
| 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 | |
| } |
This file contains hidden or 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
| 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