复制两个不同大小的元组

Copy two tuples with different sizes

我正在试验一些元组,但我发现自己在问这个问题时处于奇怪的位置:如何复制两个大小不同的元组?当然,这是为了限制两个元组的最小长度。

因此,例如,让我们创建三个元组:

std::tuple<int, char, float> a(-1, 'A', 3.14);

std::tuple<int, char, double> b = a;

std::tuple<long, int, double, char> c;

现在,ab 的类型不同,并且赋值有效(很明显)。至于 ac 事情变得有点混乱。

我的第一个实现失败了,因为我不知道如何递归具有特定类型的可变参数模板,所以像这样的东西是行不通的:

template <class T, class U>
void cp(std::tuple<T> from, std::tuple<U> to)
{

}

template <class T, class... ArgsFrom, class U, class... ArgsTo>
void cp(std::tuple<T, ArgsFrom...> from, std::tuple<U, ArgsTo...> to)
{
    std::get<0>(to) = std::get<0>(from);
    // And how to generate the rest of the tuples? 
}

那个函数不会做任何事情。所以我设计了第二次 失败 尝试,不使用类型,而是使用大小:

template<class From, class To, std::size_t i>
void copy_tuple_implementation(From &from, To &to)
{
    std::get<i>(to) = std::get<i>(from);

    copy_tuple_implementation<From, To, i - 1>(from, to);
}

template<>
void copy_tuple_implementation<class From, class To, 0>(From &from, To &to)
{
}

template<class From, class To>
void copy_tuple(From &from, To &to)
{
    constexpr std::size_t from_len = std::tuple_size<From>::value;
    constexpr std::size_t to_len   = std::tuple_size<To>::value;

    copy_tuple_implementation<From, To, from_len < to_len ? from_len - 1 : to_len - 1>(from, to);
}

但是那不会编译。我有太多错误无法在此处显示,但最重要的错误是:

Static_assert failed "tuple_element index out of range"
No type named 'type' in 'std::__1::tuple_element<18446744073709551612, std::__1::__tuple_types<> >'
Read-only variable is not assignable
No viable conversion from 'const base' (aka 'const __tuple_impl<typename __make_tuple_indices<sizeof...(_Tp)>::type, int, int, double>') to 'const __tuple_leaf<18446744073709551615UL, type>'

有趣的部分是索引超出范围,而且我无法使用 std::get<> 复制元素。

谁能帮我解决这个问题?

谢谢!

这是一种可能性,使用 C++14 的现成整数序列模板(但如果您的库不包含它,这很容易手动复制):

#include <tuple>
#include <utility>

template <std::size_t ...I, typename T1, typename T2>
void copy_tuple_impl(T1 const & from, T2 & to, std::index_sequence<I...>)
{
    int dummy[] = { (std::get<I>(to) = std::get<I>(from), 0)... };
    static_cast<void>(dummy);
}

template <typename T1, typename T2>
void copy_tuple(T1 const & from, T2 & to)
{
    copy_tuple_impl(
        from, to,
        std::make_index_sequence<std::tuple_size<T1>::value>());
}

示例:

#include <iostream>

int main()
{
    std::tuple<int, char> from { 1, 'x' };
    std::tuple<int, char, bool> to;
    copy_tuple(from, to);
    std::cout << "to<0> = " << std::get<0>(to) << "\n";
}

另一种选择是使用运算符重载来模拟函数的部分特化:

template <std::size_t N>
struct size_t_t {};

template<class From, class To, std::size_t i>
void copy_tuple_implementation(From &from, To &to, size_t_t<i>)
{
    std::get<i>(to) = std::get<i>(from);

    copy_tuple_implementation(from, to, size_t_t<i-1>{});
}

template<class From, class To>
void copy_tuple_implementation(From &from, To &to, size_t_t<0>)
{
    std::get<0>(to) = std::get<0>(from);
}

或者你可以使用助手 class:

template<class From, class To, std::size_t i>
struct CopyTuple
{
    static void run(From &from, To &to)
    {
        std::get<i>(to) = std::get<i>(from);

        CopyTuple<From,To,i-1>::run(from, to);
    }
};

template<class From, class To>
struct CopyTuple<From,To,0>
{
    static void run(From &from, To &to)
    {
        std::get<0>(to) = std::get<0>(from);
    }
};

此处的目标是在使用时获得清晰的语法。

我定义了 auto_slice,它接受一个元组,并自动将其切片用于表达式。

预期用途是

 auto_slice(lhs)=auto_slice(rhs);

而且效果很好。

// a helper that is a slightly more conservative `std::decay_t`:
template<class T>
using cleanup_t = std::remove_cv_t< std::remove_reference_t< T > >;

// the workhorse.  It holds a tuple and in an rvalue context
// allows partial assignment from and to:
template<class T,size_t s0=std::tuple_size<cleanup_t<T>>{}>
struct tuple_slicer{
  T&&t;
  // Instead of working directly within operators, the operators
  // call .get() and .assign() to do their work:
  template<class Dest,size_t s1=std::tuple_size<Dest>{}>
  Dest get() && {
    // get a pack of indexes, and use it:
    using indexes=std::make_index_sequence<(s0<s1)?s0:s1>;
    return std::move(*this).template get<Dest>(indexes{});
  }
  template<class Dest,size_t s1=std::tuple_size<Dest>{},size_t...is>
  Dest get(std::index_sequence<is...>) && {
    // We cannot construct a larger tuple from a smaller one
    // as we do not know what to populate the remainder with.
    // We could default construct them, I guess?
    static_assert(s0>=s1,"use auto_slice on target");
    using std::get;
    return Dest{ get<is>(std::forward<T>(t))... };
  }
  // allows implicit conversion from the slicer:
  template<class Dest>
  operator Dest()&&{
    return std::move(*this).template get<Dest>();
  }
  // now we are doing the assignment work.  This function
  // does the pack expansion hack, excuse the strangeness of the
  // code in it:
  template<class Src, size_t...is>
  void assign(std::index_sequence<is...>,tuple_slicer<Src>&&rhs)&&{
    using std::get;
    int _[]={0,(void(
      get<is>(std::forward<T>(t))=get<is>(std::forward<Src>(rhs.t))
    ),0)...};
    (void)_; // remove warnings
  }
  // assign from another slicer:
  template<class Src,size_t s1>
  void operator=(tuple_slicer<Src,s1>&&rhs)&&{
    using indexes=std::make_index_sequence<(s0<s1)?s0:s1>;
    std::move(*this).assign(indexes{},std::move(rhs));
  }
  // assign from a tuple.  Here we pack it up in a slicer, and use the above:
  template<class Src>
  void operator=(Src&& src)&&{
      std::move(*this) = tuple_slicer<Src>{ std::forward<Src>(src) };
  }
};

// this deduces the type of tuple_slicer<?> we need for us:
template<class Tuple>
tuple_slicer<Tuple> auto_slice(Tuple&&t){
  return {std::forward<Tuple>(t)};
}

仅在较小的一侧需要切片,但如果需要,可以在两侧(对于通用代码)进行切片。

它也适用于建筑业。在右侧,它应该与 std::arrays 和对和元组一起使用。在左侧,由于需要使用 {{}}.

构造,它可能不适用于数组

live example

这是您最初试图找出的递归解决方案:

#include <tuple>

// Limit case
template<std::size_t I = 0, typename ...From, typename ...To>
typename std::enable_if<(I >= sizeof...(From) || I >= sizeof...(To))>::type
copy_tuple(std::tuple<From...> const & from, std::tuple<To...> & to) {}

// Recursive case
template<std::size_t I = 0, typename ...From, typename ...To>
typename std::enable_if<(I < sizeof...(From) && I < sizeof...(To))>::type
copy_tuple(std::tuple<From...> const & from, std::tuple<To...> & to) 
{
    std::get<I>(to) = std::get<I>(from);
    copy_tuple<I + 1>(from,to);

}

你不需要std::index_sequence或类似的设备,而这 解决方案具有您接受的解决方案所没有的两个优点:

  • fromto 长时,它会编译并做正确的事情: from 的多余尾随元素将被忽略。
  • 它会编译,并做正确的事情,当 fromto 是一个 空元组:操作是空操作。

将其添加到此示例中:

#include <iostream>

int main()
{
    std::tuple<int, char> a { 1, 'x' };
    std::tuple<int, char, bool> b;

    // Copy shorter to longer
    copy_tuple(a, b);
    std::cout << "b<0> = " << std::get<0>(b) << "\n";
    std::cout << "b<1> = " << std::get<1>(b) << "\n";
    std::cout << "b<2> = " << std::get<2>(b) << "\n\n";

    // Copy longer to shorter   
    std::get<0>(b) = 2;
    std::get<1>(b) = 'y';
    copy_tuple(b,a);
    std::cout << "a<0> = " << std::get<0>(a) << "\n";
    std::cout << "a<1> = " << std::get<1>(a) << "\n\n";

    // Copy empty to non-empty  
    std::tuple<> empty; 
    copy_tuple(empty,a);
    std::cout << "a<0> = " << std::get<0>(a) << "\n";
    std::cout << "a<1> = " << std::get<1>(a) << "\n\n";

    // Copy non-empty to empty
    copy_tuple(a,empty);
    return 0;
}

(g++ 4.9/clang 3.5, -std=c++11)