title: Returning a reference of STL container element
body: <p>A blog I came across at <a href="http://developer-resource.blogspot.com.au/2009/01/pros-and-cons-of-returing-references.html" rel="nofollow">http://developer-resource.blogspot.com.au/2009/01/pros-and-cons-of-returing-references.html</a> writes:</p>
  
  <blockquote>
    <p>After working in this code base for a while now I believe that
    returning references are evil and should be treated just like
    returning a pointer, which is avoid it.</p>
    
    <p>For example the problem that arose that took a week to debug was the
    following:</p>
  </blockquote>
  
  <pre><code>class Foo {
          std::vector&lt; Bar &gt; m_vec;
        public:
          void insert(Bar&amp; b) { m_vec.push_back(b); }
          Bar const&amp; getById(int id) { return m_vec[id]; }
        }
  </code></pre>
  
  <blockquote>
    <p>The problem in this example is clients are calling and getting
    references that are stored in the vector. Now what happens after
    clients insert a bunch of new elements? The vector needs to resize
    internally and guess what happens to all those references? That's
    right there invalid. This caused a very hard to find bug that was
    simply fixed by removing the &amp;.</p>
  </blockquote>
  
  <p>I can't see anything wrong with the code. Am I misunderstanding return by reference &amp; STL containers, or is the post incorrect?</p>