title: Const behavior in operator >>
body: <p>Let's consider the following code (live at: <a href="http://ideone.com/3Ky4Kr" rel="nofollow">http://ideone.com/3Ky4Kr</a>)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  
  class StrStrTest {
  public:
      StrStrTest(const std::string&amp; ba) {
          a = (char*)calloc(1, ba.length() + 1);
          strcpy(a, ba.c_str());
      }
      virtual ~StrStrTest() {
          free(a);
      }
  private:
      char* a;
      friend std::basic_ostream&lt;char&gt;&amp; operator &lt;&lt; (std::basic_ostream&lt;char&gt;&amp; ss, const StrStrTest&amp; a);
      friend std::basic_istream&lt;char&gt;&amp; operator &gt;&gt; (std::basic_istream&lt;char&gt;&amp; ss,const StrStrTest&amp; a);
  };
  std::basic_ostream&lt;char&gt;&amp; operator &lt;&lt; (std::basic_ostream&lt;char&gt;&amp; ss, const StrStrTest&amp; a) {
      ss &lt;&lt; a.a;
      return ss;
  }
  std::basic_istream&lt;char&gt;&amp; operator &gt;&gt; (std::basic_istream&lt;char&gt;&amp; ss,
                                         const StrStrTest&amp; a) {
      ss &gt;&gt; a.a;  // &lt;&lt;-- HERE
  //    a.a = NULL;
      return ss;
  }
  int main()
  {
      StrStrTest bb("foo");
      std::cin &gt;&gt; bb;
      std::cout &lt;&lt; bb;
  }
  </code></pre>
  
  <p>Firstly, why does it compile? On the line marked with <strong>&lt;&lt;-- HERE</strong> I (subtly) modify a <code>const</code> object. (obviously <code>a.a = NULL;</code> does not compile, that's too obvious).</p>
  
  <p>Secondly does this lead to undefined behaviour?</p>
  
  <p>PS: Please don't consider that the code is not safe, might overwrite memory it does not own, <code>char*</code> vs. <code>std::string</code>, etc... I know these, and it is not the point of the question, this is not production code.</p>