title: Copy-assigning to a tuple from an iterator range in C++17
body: <p>I've been hoping to use new C++17 features like fold expressions and <code>std::apply()</code> to simplify some C++11 code I have that uses tools like <code>std::index_sequence</code> and <code>std::initializer_list</code> for some operations on tuples. One task in particular is giving me a bit of trouble: copying a range of values (e.g. from a <code>boost::tokenizer</code> object) to a tuple. I have a working solution that calls <code>std::apply()</code> on the tuple to which the values are to be assigned, but which still has to use <code>std::initializer_list</code> internally (for simplicity I've replaced the boost tokenizer iterators with a simple vector:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;tuple&gt;
  
  template&lt;typename Tuple, typename Iterator&gt;
  size_t copy_range_to_tuple(Tuple&amp; tup, Iterator begin, Iterator end) {
      size_t count = 0;
      auto copy = [&amp;begin,&amp;end,&amp;count] (auto&amp; value) -&gt; bool {
          if (begin != end) {
              value = *(begin++);
              ++count;
              return true;
          } else {
              return false;
          }
      };
      std::apply([&amp;copy](auto&amp;... values) {
          std::initializer_list&lt;bool&gt;{copy(values)...};
      }, tup);
      return count;
  }
  
  int main(int,char**) {
      std::tuple&lt;int,int,int,int&gt; tup;
      std::vector&lt;int&gt; x{4,3,2};
      std::cout &lt;&lt; &quot;copy count = &quot; &lt;&lt; copy_range_to_tuple(tup, x.begin(), x.end()) &lt;&lt; std::endl;
      std::cout &lt;&lt; std::get&lt;0&gt;(tup) &lt;&lt; std::endl;
      std::cout &lt;&lt; std::get&lt;1&gt;(tup) &lt;&lt; std::endl;
      std::cout &lt;&lt; std::get&lt;2&gt;(tup) &lt;&lt; std::endl;
      std::cout &lt;&lt; std::get&lt;3&gt;(tup) &lt;&lt; std::endl;
      
  }
  </code></pre>
  <p>OUTPUT:</p>
  <pre><code>copy count = 3
  4
  3
  2
  0
  </code></pre>
  <p>This is already a big win versus my C++11/14 code, which (due to the absence of <code>std::apply()</code>) used index sequences to match the iterators to the different tuple elements. However, I still have a feeling, or hope, that this could be simplified further with a fold expression, eliminating the need for initializer list. To be precise, I was hoping that I could make a fold expression that expanded <code>copy(values)</code>. Is that possible, or is there some other trick with modern C++ to make this less verbose? (Getting rid of the <code>copy</code> lambda would also be nice, but I think something like it is inevitable in order check for valid iterators).</p>