是否可以使用 Boost.Hana 将 std::array 解压缩到非类型模板参数包中


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中是否有一种惯用的方法可以做到这一点?

我认为自然的异构型 Boost.Hana 方法是将std::array元素提升为 hana::integral_constant s 的hana::tuple,并将类模板提升为元函数:

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;

相关内容

最新更新