用于可变模板实例化的Boost::hana元组解包

boost::hana tuple unpacking for variadic template instantiation

本文关键字:hana 元组 Boost 实例化 用于      更新时间:2023-10-16

关于这个问题,我想知道这样的事情是否可以使用boost::hana:

直接实现
#include <boost/hana.hpp>
#include <boost/hana/unpack.hpp>
namespace hana = boost::hana;
template<typename ... T>
struct A {};
int main() {
  auto my_tuple = hana::tuple_t<int, double, float>;
  // Is there any way to unpack my_tuple as template arguments for A?
  // Something like
  using MyStruct = A<hana::unpack_types(my_tuple)...>;
  static_assert(std::is_same<MyStruct, A<int, double, float>>::value, "Ooops!");
}

使用template_A提升为元函数,然后调用unpack:

using MyStruct = decltype(hana::unpack(my_tuple, hana::template_<A>))::type;

你可以自己做:

 template <template <typename...> class C, typename Tuple> struct RebindImpl;
 template <template <typename...> class C, typename ... Ts>
 struct RebindImpl<C, hana::tuple_t<Ts...>>{
     using type = C<Ts...>;
};
 template <template <typename...> class C, typename Tuple> 
 using Rebind = typename RebindImpl<C, Tuple>::type;

如果您只关心获得正确的类型,那么这是一种简单的方法:

template <typename... Args>
constexpr A<Args...> make_A(hana::tuple_t<Args...>) {
    return {};
}