// Generic C++ recursion: fold an iterator range with a supplied operation. // Build: c++ -std=c++17 -Wall -Wextra -Wpedantic cpp_recursive_fold.cpp -o cpp_recursive_fold #include #include #include #include #include template T recursive_fold(It first, It last, T initial, Op op, std::size_t depth = 0U) { constexpr std::size_t max_depth = 4096U; if (first == last) return initial; if (depth >= max_depth) throw std::length_error("recursive_fold depth limit exceeded"); T next = op(initial, *first); ++first; return recursive_fold(first, last, next, op, depth + 1U); } int main() { const std::vector values{2, 4, 6, 8}; const auto sum = recursive_fold(values.begin(), values.end(), 0LL, [](long long total, int value) { return total + value; }); std::cout << "fold sum = " << sum << '\n'; }