title: How to deal with reference arguments that may point to internal data?
body: <p>More than once I've tracked down a rather baffling bug only to find it was due to a <code>const</code> reference parameter changing value part way through a method. This has always been the result of a method receiving a reference argument that happens to refer to (part of) one of its own data members. So when the method mutates that member, the reference (despite being <code>const</code>) is mutated as well.</p>
  
  <p>A simple example of what I'm talking about:</p>
  
  <pre><code>#include &lt;algorithm&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  class RecentStringBuffer
  {
  public:
      const std::string &amp; at(size_t index)
      {
          return v.at(index);
      }
  
      void add(const std::string &amp; s)
      {
          // Remove any previous occurances
          v.erase(std::remove(v.begin(), v.end(), s), v.end());
          // Prepend the new entry
          v.insert(v.begin(), s);
          // Truncate older entries
          v.resize(std::min&lt;size_t&gt;(v.size(), maxEntries));
      }
  
  private:
      const int maxEntries = 10;
      std::vector&lt;std::string&gt; v;
  };
  
  int main()
  {
      RecentStringBuffer r;
      r.add("A");     // r == [A]
      r.add("B");     // r == [B, A]
      r.add("C");     // r == [C, B, A]
      r.add(r.at(1)); // r == [B, C, A] one would assume?
  
      std::cout &lt;&lt; r.at(0) &lt;&lt; r.at(1) &lt;&lt; r.at(2); // Prints "A C A"
  }
  </code></pre>
  
  <p>In this example, we get a surprising result, but if <code>v</code> had reallocated, the reference would have been left pointing outside of <code>v</code>'s memory, which would have been much worse. Technically, the reference was invalidated either way, so what ever happens is undefined behaviour.</p>
  
  <p>Similar scenarios can obviously happen with global variables instead of data members and with pointers instead of references, but members and references generally <em>feel</em> safer, so this seems like a much more surprising pitfall.</p>
  
  <p>Now, I'm not asking why this happens (I understand what's going on) or how to work around it (there are several obvious ways). My questions are more related to best practices:</p>
  
  <ul>
  <li>Is there a name for this problem?</li>
  <li>Who is responsible for worrying about this problem?</li>
  <li>Or said another way, where does the bug lie in the above application? When viewed individually, it seems perfectly reasonable and beneficial for <code>at()</code> to return a <code>const</code> reference and for <code>add()</code> to accept one. On the other hand, it doesn't seem all that fair to say that the caller, <code>main()</code>, should have known better, either, especially considering that the reference may be passed down through multiple functions before the problem occurs.</li>
  <li>Are there any general strategies for noticing and avoiding such constructs?</li>
  </ul>