3
template<int...>
struct S {};

constexpr auto a = std::array{0, 1, 2};

I would like to unpack the elements of a as template arguments to S. That is; S<0, 1, 2>.

A possible C++2a implementation:

template<auto tuple_like, template<auto...> typename Template>
constexpr decltype(auto) unpack()
{
    constexpr auto size = std::tuple_size_v<decltype(tuple_like)>;
    return []<std::size_t... Is>(std::index_sequence<Is...>) {
        return Template<std::get<Is>(tuple_like)...>{};
    }(std::make_index_sequence<size>{});
}

using Result = decltype(unpack<a, S>());

Is there an idiomatic way to do this in Boost.Hana?

invexed
  • 645
  • 3
  • 10

1 Answers1

1

I suppose the natural heterogeneous-type Boost.Hana approach is to lift the std::array elements into a hana::tuple of hana::integral_constants and also lift the class template to a metafunction:

template<int...>
struct S {};

using namespace boost::hana::literals;
constexpr auto a = boost::hana::make_tuple(0_c, 1_c, 2_c);

template<template<auto...> typename Template>
constexpr auto lift_non_type_template = []<typename... Ts>(Ts...) {
    return boost::hana::type_c<Template<Ts::value...>>;
};

using Result = decltype(boost::hana::unpack(a, lift_non_type_template<S>))::type;
invexed
  • 645
  • 3
  • 10