【问题标题】:convert `boost::tuple` to `boost::fusion::tuple`将`boost::tuple`转换为`boost::fusion::tuple`
【发布时间】:2018-10-05 14:10:42
【问题描述】:

我需要将boost::tuple 转换为对应的boost::fusion::tuple。我已经弄清楚了对应的类型。

但我希望有一个内置函数可以做到这一点。我真的不想重新发明这些东西。我在 boost fusion 文档中进行了搜索,但没有找到任何内容。

【问题讨论】:

    标签: c++ boost tuples boost-fusion


    【解决方案1】:

    你可能会使用类似的东西:

    template <class Tuple>
    auto to_fusion(Tuple&& tuple)
    {
        std::apply(
            [](auto&&... args){
                return boost::fusion::make_tuple(decltype(args)(args)...);
            },
            std::forward<Tuple>(tuple));
    }
    

    【讨论】:

    【解决方案2】:

    版本:

    template<std::size_t...Is, class T>
    auto to_fusion( std::index_sequence<Is...>, T&& in ) {
      using std::get;
      return boost::fusion::make_tuple( get<Is>(std::forward<T>(in))... );
    }
    template<class...Ts>
    auto to_fusion( boost::tuple<Ts...> in ) {
      return to_fusion( std::make_index_sequence<::boost::tuples::length< boost::tuple<Ts...>>::value>{}, std::move(in) );
    }
    template<class...Ts>
    boost::fusion::tuple<Ts...> to_fusion( std::tuple<Ts...> in ) {
      return to_fusion( std::make_index_sequence<sizeof...(Ts)>{}, std::move(in) );
    }
    

    我不知道有内置版本。

    中添加尾随-&gt; decltype(boost::fusion::make_tuple( get&lt;Is&gt;(std::forward&lt;T&gt;(in))... ))。你还需要make_index_sequence,它可能有一个 boost 等价物。

    Live example.

    【讨论】:

    • 应该Ts...Is... 吗?即使我将Ts 更改为Is,我也得到type/value 不匹配
    • @NeelBasu 对不起,错字。
    • @NeelBasu 我依靠 ADL 找到正确的 get。我只是引入了一个“已知”的模板获取,因此在 ADL 之前编译器将其视为模板。
    • 我收到/usr/include/boost/tuple/detail/tuple_basic.hpp:133:33: error: no type named ‘head_type’ in ‘boost::tuples::detail::drop_front&lt;2&gt;::apply&lt;boost::tuples::cons&lt;point_2d&lt;double&gt;, boost::tuples::cons&lt;point_2d&lt;double&gt;, boost::tuples::null_type&gt; &gt; &gt;::type’ {aka ‘struct boost::tuples::null_type’}
    • make_index_sequence&lt;sizeof...(Ts)&gt; -> index_sequence_for&lt;Ts...&gt;?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    相关资源
    最近更新 更多