From e546e19a6376dab6545ae833a7f54fb23ddef501 Mon Sep 17 00:00:00 2001 From: Drew Hubley Date: Tue, 18 Nov 2025 20:09:11 -0400 Subject: [PATCH] Updated for_each to use a fold over recurssion --- include/xtensor/utils/xutils.hpp | 51 +++++++++++--------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/include/xtensor/utils/xutils.hpp b/include/xtensor/utils/xutils.hpp index 8f40b0c05..85ef31d5d 100644 --- a/include/xtensor/utils/xutils.hpp +++ b/include/xtensor/utils/xutils.hpp @@ -163,52 +163,37 @@ namespace xt namespace detail { - template - inline typename std::enable_if::type - for_each_impl(F&& /*f*/, std::tuple& /*t*/) noexcept + template + void for_each(F&& f, std::tuple& t, std::index_sequence) noexcept( + (noexcept(f(std::get(t))) && ...) + ) { + (f(std::get(t)), ...); } - template - inline typename std::enable_if < I::type - for_each_impl(F&& f, std::tuple& t) noexcept(noexcept(f(std::get(t)))) + template + void for_each(F&& f, const std::tuple& t, std::index_sequence) noexcept( + (noexcept(f(std::get(t))) && ...) + ) { - f(std::get(t)); - for_each_impl(std::forward(f), t); + (f(std::get(t)), ...); } } - template - inline void for_each(F&& f, std::tuple& t) noexcept( - noexcept(detail::for_each_impl<0, F, T...>(std::forward(f), t)) + template + inline void for_each(F&& f, std::tuple& t) noexcept( + noexcept(detail::for_each(std::forward(f), t, std::make_index_sequence{})) ) { - detail::for_each_impl<0, F, T...>(std::forward(f), t); + detail::for_each(std::forward(f), t, std::make_index_sequence{}); } - namespace detail - { - template - inline typename std::enable_if::type - for_each_impl(F&& /*f*/, const std::tuple& /*t*/) noexcept - { - } - - template - inline typename std::enable_if < I::type - for_each_impl(F&& f, const std::tuple& t) noexcept(noexcept(f(std::get(t)))) - { - f(std::get(t)); - for_each_impl(std::forward(f), t); - } - } - - template - inline void for_each(F&& f, const std::tuple& t) noexcept( - noexcept(detail::for_each_impl<0, F, T...>(std::forward(f), t)) + template + inline void for_each(F&& f, const std::tuple& t) noexcept( + noexcept(detail::for_each(std::forward(f), t, std::make_index_sequence{})) ) { - detail::for_each_impl<0, F, T...>(std::forward(f), t); + detail::for_each(std::forward(f), t, std::make_index_sequence{}); } /*****************************