title: C++: Unpack a tuple with references and values without copying/moving too much
body: <p>Let's say I have a tuple <code>t</code> whose type is <code>std::tuple&lt;T1,T2,T3&gt;</code> where each <code>T</code> may be either <code>Obj</code>, <code>Obj&amp;</code>, <code>Obj&amp;&amp;</code>,<code>const Obj&amp;</code>. I want to write a function which unpacks the values of the tuple into a function <code>f</code> which accepts three types that decay to <code>Obj</code>. I want to avoid useless copying as much as possible, how I do it?</p>
  
  <p>My current implementation is</p>
  
  <pre><code>static R call(C2&amp;&amp; c, TUP&amp;&amp; x) {
  using TUP0 = typename std::remove_reference&lt;TUP&gt;::type;
  return c(std::forward&lt;typename std::tuple_element&lt;0, TUP0&gt;::type&gt;(std::get&lt;0&gt;(x)),
           std::forward&lt;typename std::tuple_element&lt;1, TUP0&gt;::type&gt;(std::get&lt;1&gt;(x)),
           std::forward&lt;typename std::tuple_element&lt;2, TUP0&gt;::type&gt;(std::get&lt;2&gt;(x)));
  }
  </code></pre>
  
  <p>But this implementation seems to <code>move</code> things when <code>TUP</code> is <code>std::tuple&lt;Obj,Obj,Obj&gt;</code>, even though it should only move when TUP contains an <code>Obj&amp;&amp;</code>.</p>