title: Inserting into vector by reference to element of same vector
body: <p>I was wondering if someone more experienced might be able to clarify if this is a buggy operation to do on a vector:</p>
  
  <pre><code>std::vector&lt;int&gt; v{1, 2, 3, 4, 5};
  v.insert(v.begin() + 1, v[0]);
  </code></pre>
  
  <p>The reason I ask is because the element to insert is a reference to the 0th element in the vector. If the insertion forces the vector to resize (because its capacity is full), then the reference to <code>v[0]</code> would be invalidated, and the code might insert an incorrect value. Here is some pseudo-code  that might demonstrate:</p>
  
  <pre><code>template &lt;typename T&gt;
  void vector_insert_method(Iterator pos, const T&amp; value) {
      if capacity full:
          reallocate array and copy over element
          // if value was reference to elem in this vector,
          // that reference might be invalidated
  
  
      insert element
  
      ++size
  }
  </code></pre>
  
  <p>This issue might be more realistic on a concurrent system.</p>
  
  <p>A similar, and related question is what happens if you try inserting an element that comes after the position you are trying to insert. For example, doing something like <code>v.insert(v.begin(), v[2])</code> because the standard points out that references to elements after the insertion point are invalidated. Is this guaranteed to work?</p>