@@ -55,7 +55,7 @@ namespace xt
5555 constexpr decltype (auto ) argument(Args&&... args) noexcept ;
5656
5757 template <class R , class F , class ... S>
58- R apply (std::size_t index, F&& func, const std::tuple<S...>& s) NOEXCEPT( noexcept (func(std::get< 0 >(s)))) ;
58+ R apply (std::size_t index, F&& func, const std::tuple<S...>& s);
5959
6060 template <class T , class S >
6161 void nested_copy (T&& iter, const S& s);
@@ -283,29 +283,31 @@ namespace xt
283283 * apply implementation *
284284 ************************/
285285
286- namespace detail
287- {
288- template <class R , class F , std::size_t I, class ... S>
289- R apply_one (F&& func, const std::tuple<S...>& s) NOEXCEPT(noexcept (func(std::get<I>(s))))
290- {
291- return static_cast <R>(func (std::get<I>(s)));
292- }
293-
294- template <class R , class F , std::size_t ... I, class ... S>
295- R apply (std::size_t index, F&& func, std::index_sequence<I...> /* seq*/ , const std::tuple<S...>& s)
296- NOEXCEPT(noexcept (func(std::get<0 >(s))))
297- {
298- using FT = std::add_pointer_t <R (F&&, const std::tuple<S...>&)>;
299- static const std::array<FT, sizeof ...(I)> ar = {{&apply_one<R, F, I, S...>...}};
300- return ar[index](std::forward<F>(func), s);
301- }
302- }
303-
304286 template <class R , class F , class ... S>
305287 inline R apply (std::size_t index, F&& func, const std::tuple<S...>& s)
306- NOEXCEPT(noexcept (func(std::get<0 >(s))))
307288 {
308- return detail::apply<R>(index, std::forward<F>(func), std::make_index_sequence<sizeof ...(S)>(), s);
289+ return std::apply (
290+ [&](const S&... args) -> R
291+ {
292+ return [&](const auto &... t) -> R
293+ {
294+ auto f_impl = [&](auto && self, auto && i, auto && h, auto &&... t) -> R
295+ {
296+ if (i == index)
297+ {
298+ return func (h);
299+ }
300+ if constexpr (sizeof ...(t) > 0 )
301+ {
302+ return self (self, std::size_t {i + 1 }, t...);
303+ }
304+ XTENSOR_THROW (std::runtime_error, " Index applied to tuple of insufficient length" );
305+ };
306+ return f_impl (f_impl, std::size_t {0 }, t...);
307+ }(args...);
308+ },
309+ s
310+ );
309311 }
310312
311313 /* **************************
0 commit comments