Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::hana tuple unpacking for variadic template instantiation

Related to this question I was wondering if something like this would be achievable in a straightforward way using 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!");
}
like image 230
Pablo Antolín Avatar asked Dec 08 '22 22:12

Pablo Antolín


2 Answers

You may do it yourself with:

 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;
like image 25
Jarod42 Avatar answered Dec 29 '22 13:12

Jarod42


Use template_ to lift A into a metafunction, then call unpack:

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

Example.

like image 166
ecatmur Avatar answered Dec 29 '22 12:12

ecatmur