{"slug": "c-metaprogramming-knuth-s-arrows", "title": "C++ metaprogramming Knuth's arrows", "summary": "This article presents two C++ implementations of Knuth's up-arrow notation for representing extremely large numbers through repeated exponentiation. The first implementation uses `constexpr` functions with recursion, while the second uses template metaprogramming with specialized structs, both capable of computing values like 3↑↑3 (which equals 7,625,597,484,987).", "body_md": "#Knuth arrows in C++ metaprogramming.\nTwo implementations of Knuth's arrows using constexpr\nand template metaprogramming.\n#include <stdint.h>\n#include <stdexcept>\nconstexpr uint64_t pow(uint64_t a, uint64_t n)\n{\nreturn\nn == 0\n? 1\n: a * pow(a, n - 1);\n}\nconstexpr uint64_t tower(uint64_t a, uint64_t b)\n{\nreturn\nb == 0\n? throw std::invalid_argument(\"b == 0\")\n: b == 1\n? a\n: pow(a, tower(a, b - 1));\n}\nconstexpr uint64_t arr(uint64_t a, uint64_t b, uint64_t s);\nconstexpr uint64_t arr2(uint64_t cnt, uint64_t a, uint64_t sp, uint64_t b)\n{\nreturn cnt == 0\n? arr(a, sp, b)\n: arr(a, arr2(cnt - 1, a, sp, b), sp);\n}\nconstexpr uint64_t arr(uint64_t a, uint64_t s, uint64_t b)\n{\nreturn s == 1\n? pow(a, b)\n: s == 2\n? tower(a, b)\n: arr2(b - 1, a, s - 1, a);\n}\nconstexpr uint64_t arr_impl(bool arr_iter, uint64_t cnt, uint64_t a, uint64_t s, uint64_t b)\n{\nreturn arr_iter\n? (cnt == 0\n? arr_impl(false, 0, a, s, b)\n: arr_impl(false, 0, a, arr_impl(true, cnt - 1, a, s, b), s))\n: (s == 1\n? pow(a, b)\n: s == 2\n? tower(a, b)\n: arr_impl(true, b - 1, a, s - 1, a));\n}\nconstexpr uint64_t arr(uint64_t a, uint64_t s, uint64_t b)\n{\nreturn arr_impl(false, 0, a, s, b);\n}\nuint64_t x = arr(3, 2, 3); // 3 ↑↑ 3 = 7625597484987\nuint64_t x = arr(2, 2, 3); // 2 ↑↑ 3 = 16\n(Resembles the variant #2 of constexpr Knuth arrows)\n#include <stdint.h>\n/// Pow\ntemplate <uint64_t A, uint64_t N>\nstruct Pow {\nstatic constexpr uint64_t value = A * Pow<A, N - 1>::value;\n};\ntemplate <uint64_t A>\nstruct Pow<A, 0> {\nstatic constexpr uint64_t value = 1;\n};\n// Tower\ntemplate <uint64_t A, uint64_t B>\nstruct Tower {\nstatic constexpr uint64_t value = Pow<A,\nTower<A, B - 1>::value\n>::value;\n};\ntemplate <uint64_t A>\nstruct Tower<A, 1> {\nstatic constexpr uint64_t value = A;\n};\n// ArrImpl\ntemplate <bool arr_iter, uint64_t C, uint64_t A, uint64_t S, uint64_t B>\nstruct ArrImpl {\nstatic constexpr uint64_t value = 1;\n};\ntemplate <uint64_t C, uint64_t A, uint64_t S, uint64_t B>\nstruct ArrImpl<true, C, A, S, B> {\nstatic constexpr uint64_t value\n= ArrImpl<false, 0, A,\nArrImpl<true, C - 1, A, S, B>::value,\nS>::value;\n};\ntemplate <uint64_t A, uint64_t S, uint64_t B>\nstruct ArrImpl<true, 0, A, S, B> {\nstatic constexpr uint64_t value\n= ArrImpl<false, 0, A, S, B>::value;\n};\ntemplate <uint64_t C, uint64_t A, uint64_t B>\nstruct ArrImpl<false, C, A, 1, B> {\nstatic constexpr uint64_t value\n= Pow<A, B>::value;\n};\ntemplate <uint64_t C, uint64_t A, uint64_t B>\nstruct ArrImpl<false, C, A, 2, B> {\nstatic constexpr uint64_t value\n= Tower<A, B>::value;\n};\ntemplate <uint64_t C, uint64_t S, uint64_t A, uint64_t B>\nstruct ArrImpl<false, C, A, S, B> {\nstatic constexpr uint64_t value\n= ArrImpl<true, B - 1, A, S - 1, A>::value;\n};\n// Arr\ntemplate <uint64_t A, uint64_t S, uint64_t B>\nstruct Arr {\nstatic constexpr uint64_t value\n= ArrImpl<false, 0, A, S, B>::value;\n};\nauto T1 = Pow<2, 5>::value; // 32\nauto T2 = Tower<2, 3>::value; // 16\nauto T3 = Arr<3, 2, 3>::value; // 3 ↑↑ 3 = 7625597484987", "url": "https://wpnews.pro/news/c-metaprogramming-knuth-s-arrows", "canonical_source": "https://gist.github.com/graninas/358f9c7b80944b7e6a3fe56c6fe09a57", "published_at": "2019-01-25 14:54:53+00:00", "updated_at": "2026-05-22 19:06:09.215356+00:00", "lang": "en", "topics": ["developer-tools", "research"], "entities": [], "alternates": {"html": "https://wpnews.pro/news/c-metaprogramming-knuth-s-arrows", "markdown": "https://wpnews.pro/news/c-metaprogramming-knuth-s-arrows.md", "text": "https://wpnews.pro/news/c-metaprogramming-knuth-s-arrows.txt", "jsonld": "https://wpnews.pro/news/c-metaprogramming-knuth-s-arrows.jsonld"}}