【问题标题】:Conversion between std::tuple and boost::tuplestd::tuple 和 boost::tuple 之间的转换
【发布时间】:2015-03-24 02:03:59
【问题描述】:

给定boost::tuplestd::tuple,如何在它们之间进行转换?

换句话说,你将如何实现以下两个功能?

template <typename... T> boost::tuple<T...> asBoostTuple(  std::tuple<T...> stdTuple);
template <typename... T>   std::tuple<T...> asStdTuple  (boost::tuple<T...> boostTuple);

似乎是一个简单的事情,但我找不到任何好的解决方案。


我尝试了什么?

我最终通过模板编程解决了这个问题。它似乎适用于我的具体设置,但感觉不对(太冗长),我什至不确定它是否真的适用于所有情况(我稍后会谈到这一点)。

不管怎样,这里是有趣的部分:

template<int offset, typename... T>
struct CopyStdTupleHelper
{
    static void copy(boost::tuple<T...> source, std::tuple<T...>& target) {
        std::get<offset>(target) = std::move(boost::get<offset>(source));
        CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
    }

    static void copy(std::tuple<T...> source, boost::tuple<T...>& target) {
        boost::get<offset>(target) = std::move(std::get<offset>(source));
        CopyStdTupleHelper<offset - 1, T...>::copy(source, target);
    }
};

template<typename... T>
struct CopyStdTupleHelper<-1, T...>
{
    static void copy(boost::tuple<T...> source, std::tuple<T...>& target)
      { /* nothing to do (end of recursion) */ }

    static void copy(std::tuple<T...> source, boost::tuple<T...>& target)
      { /* nothing to do (end of recursion) */ }
};


std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
    std::tuple<T...> result;
    CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(boostTuple), result);
    return result;
}

boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
    boost::tuple<T...> result;
    CopyStdTupleHelper<sizeof...(T) - 1, T...>::copy(std::move(stdTuple), result);
    return result;
}

我想知道是否有更优雅的方式。使用boost::tuplestd::tuple 包装现有API 似乎是一种非常常见的操作。


我试图为您提供一个最小的测试示例,其中仅缺少 asBoostTupleasStdTuple 的实现。但是,对于我不完全理解的boost::tuples::null_type 的一些魔法,它无法编译。这也是我不确定我现有的解决方案是否可以普遍应用的原因。

这里是sn-p:

#include <tuple>
#include <boost/tuple/tuple.hpp>

template <typename... T>
boost::tuple<T...> asBoostTuple(std::tuple<T...> stdTuple) {
  boost::tuple<T...> result;
  // TODO: ...
  return result;
}

template <typename... T>
std::tuple<T...> asStdTuple(boost::tuple<T...> boostTuple) {
  std::tuple<T...> result;
  // TODO: ...
  return result;
}

int main() {
  boost::tuple<std::string, int, char> x = asBoostTuple(std::make_tuple("A", 1, 'x'));

  // ERROR: 
  std::tuple<std::string, int, char> y = asStdTuple<std::string, int, char>(x);

  return x == y;
}

错误信息是:

example.cpp:20:38: error: no viable conversion from 'tuple<[3 *
      ...], boost::tuples::null_type, boost::tuples::null_type,
      boost::tuples::null_type, boost::tuples::null_type,
      boost::tuples::null_type, boost::tuples::null_type,
      boost::tuples::null_type>' to 'tuple<[3 * ...], (no
      argument), (no argument), (no argument), (no argument),
      (no argument), (no argument), (no argument)>'
  ...int, char> y = asStdTuple<std::string, int, char>(x);

我了解 Boost 的实现不是基于可变参数模板,这应该可以解释 null_type,但我仍然不确定如何避免该编译错误。

【问题讨论】:

    标签: c++ boost tuples variadic-templates c++14


    【解决方案1】:

    进行此类操作时的常用技巧是构造一个整数序列,然后使用包扩展来初始化新元组。

    在这种情况下,额外的转折是null_type。为此,将元组类型视为不透明可能是最简单的,并使用boost::tuples::lengthboost::tuples::element 对其进行操作,它们已经正确处理了null_type。这样您就不必依赖于 boost 元组的实现细节。

    所以:

    template <typename StdTuple, std::size_t... Is>
    auto asBoostTuple(StdTuple&& stdTuple, std::index_sequence<Is...>) {
        return boost::tuple<std::tuple_element_t<Is, std::decay_t<StdTuple>>...>
                            (std::get<Is>(std::forward<StdTuple>(stdTuple))...);
    }
    
    template <typename BoostTuple, std::size_t... Is>
    auto asStdTuple(BoostTuple&& boostTuple, std::index_sequence<Is...>) {
        return std::tuple<typename boost::tuples::element<Is, std::decay_t<BoostTuple>>::type...>
                         (boost::get<Is>(std::forward<BoostTuple>(boostTuple))...);
    }
    
    template <typename StdTuple>
    auto asBoostTuple(StdTuple&& stdTuple) {
      return asBoostTuple(std::forward<StdTuple>(stdTuple),
                 std::make_index_sequence<std::tuple_size<std::decay_t<StdTuple>>::value>());
    }
    
    template <typename BoostTuple>
    auto asStdTuple(BoostTuple&& boostTuple) {
      return asStdTuple(std::forward<BoostTuple>(boostTuple),
                 std::make_index_sequence<boost::tuples::length<std::decay_t<BoostTuple>>::value>());
    }
    

    Demo.

    注意这两种情况下代码的基本结构完全相同:我们获取元组的大小(通过boost::tuples::lengthstd::tuple_size),使用std::make_index_sequence构造一个整数序列,然后使用整数序列,获取boost::tuples::elementstd::tuple_element的类型,boost::get/std::get的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多