【问题标题】:boost::hana tuple unpacking for variadic template instantiationboost::hana 元组解包可变参数模板实例化
【发布时间】:2016-06-24 07:50:52
【问题描述】:

this question 相关 我想知道是否可以使用 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!");
}

【问题讨论】:

标签: c++ boost c++14 boost-hana


【解决方案1】:

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

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

Example.

【讨论】:

    【解决方案2】:

    你可以自己做:

     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;
    

    【讨论】:

    • 这不会如你所愿! hana::tuple_t&lt;T...&gt; 被指定为在功能上等同于 hana::make_tuple(hana::type&lt;T&gt;{}...),除了可能具有不同的类型。所以首先,你不知道你得到的是什么类型,其次,即使你认为它hana::make_tuple(hana::type&lt;T&gt;{}...)完全相同,那么你的解决方案将实例化C&lt;hana::type&lt;T&gt;...&gt;,这不是你想要什么。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-28
      • 2014-09-13
      • 2021-06-30
      • 1970-01-01
      • 2014-06-30
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多