title: Efficient passing of std::vector
body: <p>When a C++ function accepts an <code>std::vector</code> argument, the usual pattern is to pass it by <code>const</code> reference, such as:</p>
  
  <pre><code>int sum2(const std::vector&lt;int&gt; &amp;v)
  {
     int s = 0;
     for(size_t i = 0; i &lt; v.size(); i++) s += fn(v[i]);
     return s;
  }
  </code></pre>
  
  <p>I believe that this code results in double dereferencing when the vector elements are accessed, because the CPU should first dereference <code>v</code> to read the pointer to the first element, which pointer needs to be dereferenced again to read the first element. I would expect that it would be more efficient to pass a shallow copy of the vector object on the stack. Such shallow copy would encapsulate a pointer to the first element, and the size, with the pointer referencing the same memory area as the original vector does.</p>
  
  <pre><code>int sum2(vector_ref&lt;int&gt; v)
  {
     int s = 0;
     for(size_t i = 0; i &lt; v.size(); i++) s += fn(v[i]);
     return s;
  }
  </code></pre>
  
  <p>Similar performance, but much less convenience could be achieved by passing a random access iterator pair. <em>My question is: what is flawed with this idea?</em> I expect that there should be some good reason that smart people accept to pay the performace cost of vector reference, or deal with the inconvenience of iterators.</p>
  
  <p>Edit: Based on the coments below, please consider the situation if I simply rename the suggested <code>vector_ref</code> class to <em>slice</em> or <em>range</em>. The intention is to use random-access iterator pairs with more natural syntax.</p>