aboutsummaryrefslogtreecommitdiff
path: root/entries/timstr
diff options
context:
space:
mode:
authorBraxton Hall2022-10-31 03:39:05 +0000
committerGitHub2022-10-31 03:39:05 +0000
commit25afac0b0b226d5b06b392047a9a914ab968c6a8 (patch)
treea7b96097682a77ebf4572a5782d2ceb0f727496e /entries/timstr
parentf39cf4af4da11eed7dbf6def49f37d9bac5c3346 (diff)
parent56f532b893e233603312dd212a669e1add4a2b53 (diff)
Merge pull request #73 from timstr/main
Fibonacci using C++ template meta-programming
Diffstat (limited to 'entries/timstr')
-rw-r--r--entries/timstr/fib.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/entries/timstr/fib.cpp b/entries/timstr/fib.cpp
new file mode 100644
index 0000000..08e5802
--- /dev/null
+++ b/entries/timstr/fib.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+#include <cstdint>
+
+template<std::size_t N>
+struct fib {
+ static constexpr std::size_t value = fib<N - 1>::value + fib<N - 2>::value;
+};
+
+template<>
+struct fib<0> {
+ static constexpr std::size_t value = 1;
+};
+
+template<>
+struct fib<1> {
+ static constexpr std::size_t value = 1;
+};
+
+template<std::size_t N>
+constexpr std::size_t fib_v = fib<N>::value;
+
+template<std::size_t N>
+struct print_fib : print_fib<N - 1> {
+ print_fib() noexcept {
+ std::cout << fib_v<N - 1> << '\n';
+ }
+};
+
+template<>
+struct print_fib<0> {};
+
+int main() {
+ print_fib<32>{};
+ return 0;
+} \ No newline at end of file