Boost.Hana能够将std :: array解包为非类型模板参数包吗?

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

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

我想将元素a作为模板参数传递给S,即:S<0, 1, 2>
以下是可能的C++2a实现:
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>());

Boost.Hana 中有没有惯用的方法可以做到这一点?
1个回答

1

我认为自然的异构类型Boost.Hana方法是将std::array元素提升到hana::tuplehana::integral_constant中,同时将类模板提升为元函数:

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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接