代码之家  ›  专栏  ›  技术社区  ›  Neel Basu

将“boost::tuple”转换为“boost::fusion::tuple”`

  •  0
  • Neel Basu  · 技术社区  · 7 年前

    boost::tuple 到相应的 boost::fusion::tuple . 我已经找出了相应的类型。

    但我希望有一个内置函数来实现这一点。我真的不想重新发明这种东西。我在boost fusion文档中搜索过,但没有找到任何。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Yakk - Adam Nevraumont    7 年前

    版本:

    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) );
    }
    

    我不知道有一个内置版本。

    添加尾部 -> decltype(boost::fusion::make_tuple( get<Is>(std::forward<T>(in))... )) . 你还需要 make_index_sequence ,这可能有一个助推当量。

    Live example

        2
  •  3
  •   Jarod42    7 年前

    您可以使用以下内容:

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