diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-01-17 16:23:24 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-01-17 16:23:24 +0100 |
| commit | 0b43d984905f07c59e3236d98d4e27409aba3cda (patch) | |
| tree | ef707db657a9a3ff0da21ee06ac8df65f9a561a9 /templates/factorial.cpp | |
| parent | d0f3f64bb42ea8194c20f0eadd35821cab346143 (diff) | |
| download | taming-cpp-0b43d984905f07c59e3236d98d4e27409aba3cda.tar.gz taming-cpp-0b43d984905f07c59e3236d98d4e27409aba3cda.zip | |
More stuff with templates
Diffstat (limited to '')
| -rw-r--r-- | templates/factorial.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/templates/factorial.cpp b/templates/factorial.cpp new file mode 100644 index 0000000..11e4789 --- /dev/null +++ b/templates/factorial.cpp | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | // Taken from https://en.wikipedia.org/wiki/Template_metaprogramming | ||
| 2 | |||
| 3 | #include <iostream> | ||
| 4 | |||
| 5 | template<long long N> | ||
| 6 | class factorial { | ||
| 7 | public: | ||
| 8 | static constexpr long long value = N * factorial<N-1>::value; | ||
| 9 | }; | ||
| 10 | |||
| 11 | template<> | ||
| 12 | class factorial<0> { | ||
| 13 | public: | ||
| 14 | static constexpr long long value = 1; | ||
| 15 | }; | ||
| 16 | |||
| 17 | int main() { | ||
| 18 | constexpr long long X = 12; | ||
| 19 | std::cout << factorial<X>::value << std::endl; | ||
| 20 | |||
| 21 | return 0; | ||
| 22 | } | ||
