4

与这个问题相关,我想知道这样的事情是否可以使用 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!");
}
4

3 回答 3

13

用于template_提升A到元函数,然后调用unpack

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

Example.

于 2016-06-24T09:21:23.053 回答
1

你可以自己做:

 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;
于 2016-06-24T08:15:59.563 回答
0

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

template <typename... Args>
constexpr A<Args...> make_A(hana::tuple_t<Args...>) {
    return {};
}
于 2016-06-24T08:46:06.117 回答