title: Extract input iterator from std::copy and std::copy_n
body: <p>I trying to implement an unserialization method that take an input iterator and perform a series of chunk reads (using <code>std::copy</code> and <code>std::copy_n</code>). Something like this (just an example):</p>
  
  <pre><code>template &lt;class InputIt&gt;
  InputIt unserialize(InputIt it)
  {
    std::copy_n(it, sizeof(header_type), reinterpret_cast&lt;char*&gt;(&amp;header));
    std::copy_n(it, header.payload_size, std::back_inserter(payload));
    it = optional.unserialize(it);
    return it;
  }
  </code></pre>
  
  <p>How do I advance input iterator in this case so each following call to <code>std::copy_n</code> continue read from it and I can finally return it?</p>
  
  <p>I want to be generic to iterator categories (especially RandomAccessIterator and InputIterator) for performance reasons and hope it is possible to use <code>std::copy</code> methods without need to rewrite these. Stuff like bound checking and such will be done by iterator adapter or checked before unserialization call if size is known.</p>
  
  <p>What do not work nor acceptable:</p>
  
  <ol>
  <li>Using <code>std::copy_n&lt;InputIt&amp;&gt;(it, ...)</code> may work for some categories but not for all and it's too unreliable.</li>
  <li>Using <code>std::advance</code> after each call leads to rereading same block again for some iterators. Not preferable and may be impossible for some sources.</li>
  </ol>
  
  <p><strong>UPDATE</strong> Making iterator reference adapter doesn't help as <em>random access iterator</em> version of <code>copy_n</code> return pointer to <strong>past</strong> the last element copied while <em>input iterator</em> version return pointer to the last element copied. So I guess own version of <code>copy_n</code> works best with additional iterator adapter for bound checking.</p>