title: Copy two tuples with different sizes
body: <p>I am experimenting with some tuples, and I find myself in the weird position of asking this: how can I copy two tuples that <em>differ in their sizes</em>? Of course, this is intended limited to the minimum length of the two tuples.</p>
  
  <p>So, for instance, let's create three tuples:</p>
  
  <pre><code>std::tuple&lt;int, char, float&gt; a(-1, 'A', 3.14);
  
  std::tuple&lt;int, char, double&gt; b = a;
  
  std::tuple&lt;long, int, double, char&gt; c;
  </code></pre>
  
  <p>Now, <code>a</code> and <code>b</code> differ in types, and the assignment work (obviously). As for <code>a</code> and <code>c</code> the things get a little more confusing. </p>
  
  <p>My first implementation failed, since I don't know how to recurse on variadic templates with a specific type, so something like this won't work:</p>
  
  <pre><code>template &lt;class T, class U&gt;
  void cp(std::tuple&lt;T&gt; from, std::tuple&lt;U&gt; to)
  {
  
  }
  
  template &lt;class T, class... ArgsFrom, class U, class... ArgsTo&gt;
  void cp(std::tuple&lt;T, ArgsFrom...&gt; from, std::tuple&lt;U, ArgsTo...&gt; to)
  {
      std::get&lt;0&gt;(to) = std::get&lt;0&gt;(from);
      // And how to generate the rest of the tuples? 
  }
  </code></pre>
  
  <p>That function won't do anything. So I've devised a second <em>failing</em> attempt, using not the types, but the sizes:</p>
  
  <pre><code>template&lt;class From, class To, std::size_t i&gt;
  void copy_tuple_implementation(From &amp;from, To &amp;to)
  {
      std::get&lt;i&gt;(to) = std::get&lt;i&gt;(from);
  
      copy_tuple_implementation&lt;From, To, i - 1&gt;(from, to);
  }
  
  template&lt;&gt;
  void copy_tuple_implementation&lt;class From, class To, 0&gt;(From &amp;from, To &amp;to)
  {
  }
  
  template&lt;class From, class To&gt;
  void copy_tuple(From &amp;from, To &amp;to)
  {
      constexpr std::size_t from_len = std::tuple_size&lt;From&gt;::value;
      constexpr std::size_t to_len   = std::tuple_size&lt;To&gt;::value;
  
      copy_tuple_implementation&lt;From, To, from_len &lt; to_len ? from_len - 1 : to_len - 1&gt;(from, to);
  }
  </code></pre>
  
  <p>But that won't compile. I have too many errors to display here, but the most significant ones are:</p>
  
  <pre><code>Static_assert failed "tuple_element index out of range"
  No type named 'type' in 'std::__1::tuple_element&lt;18446744073709551612, std::__1::__tuple_types&lt;&gt; &gt;'
  Read-only variable is not assignable
  No viable conversion from 'const base' (aka 'const __tuple_impl&lt;typename __make_tuple_indices&lt;sizeof...(_Tp)&gt;::type, int, int, double&gt;') to 'const __tuple_leaf&lt;18446744073709551615UL, type&gt;'
  </code></pre>
  
  <p>The interesting part is the index out of range, and the fact that I cannot copy an element with <code>std::get&lt;&gt;</code>.</p>
  
  <p>Can anyone help me in this?</p>
  
  <p>Thanks!</p>