You could write apply
which takes a reference to an array and functor to call with the unpacked array (you might want to add perfect forwarding and such):
template <typename F, typename T, std::size_t N, std::size_t... Idx>
decltype(auto) apply_impl (F f, T (&t)[N], std::index_sequence<Idx...>) {
return f(t[Idx]...);
}
template <typename F, typename T, std::size_t N>
decltype(auto) apply (F f, T (&t)[N]) {
return apply_impl(f, t, std::make_index_sequence<N>{});
}
Then call it like this, if foo
is a functor class:
apply(foo{}, a);
If foo
is just a normal template function like in your example, you could wrap it in a lambda:
apply([](auto...xs){foo(std::forward<decltype(xs)>(xs)...);}, a);
Live Demo