c++ - fixed size container to variadic template argument list conversion -
i have call variadic template function can take number of arguments.
template < class ... args > void f( args&... args);
i wish write small wrapper function can call f
n arguments of same type contained in fixed size container std::array.
the goal write like
std::array<int, 3> arr = {1,2,3}; wrapper(arr); // calls f(1,2,3);
i tried use combination of initializer lists , std::forward
no avail. there way achieve want ?
if compiler supports c++14 can following way:
template <class ... args> void f(args&&... args) { ... } template<typename t, std::size_t n, std::size_t... i> void wrapper_impl(std::array<t, n> const &arr, std::index_sequence<i...>) { f(arr[i]...); } template<typename t, std::size_t n, typename indices = std::make_index_sequence<n>> void wrapper(std::array<t, n> const &arr) { wrapper_impl(arr, indices()); }
for c++11 based on so answer code machinery , below (haven't test though):
namespace detail { template<std::size_t... is> struct seq {}; template<std::size_t n, int... is> struct gen_seq : gen_seq<n-1,n-1, is...> {}; template<std::size_t... is> struct gen_seq<0, is...> : seq<is...> {}; } template <class ... args> void f(args&&... args) { ... } template<typename t, std::size_t n, std::size_t... i> void wrapper_impl(std::array<t, n> const &arr, detail::seq<i...>) { f(arr[i]...); } template<typename t, std::size_t n> void wrapper(std::array<t, n> const &arr) { wrapper_impl(arr, detail::gen_seq<n>()); }
Comments
Post a Comment