title: Behavior of move constructor for string class members
body: <p>I'm aware that, unless otherwise specified, all standard library functions that accept rvalue reference parameters are guaranteed to leave the moved-from argument in valid but unspecified state, and that some of the examples here may exhibit undefined behavior as a result, but the underlying question does not depend on that.</p>
  
  <p>The following program:</p>
  
  <pre><code>//  testmove1.cpp
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main() {
      std::string s1{"String"};
      std::cout &lt;&lt; "s1: [" &lt;&lt; s1 &lt;&lt; "]" &lt;&lt; std::endl;
      std::string s2{std::move(s1)};
      std::cout &lt;&lt; "s2: [" &lt;&lt; s2 &lt;&lt; "]" &lt;&lt; std::endl;
      std::cout &lt;&lt; "s1 after move: [" &lt;&lt; s1 &lt;&lt; "]" &lt;&lt; std::endl;  //  Undefined
  
      return 0;
  }
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>paul@local:~$ ./testmove1
  s1: [String]
  s2: [String]
  s1 after move: []
  paul@local:~$ 
  </code></pre>
  
  <p>Outputting <code>s1</code> after the move seems undefined to me, but the string being left empty is at least a feasible alternative. Valgrind reports a single allocation being made for this program, which is what you'd expect.</p>
  
  <p>If I do something very similar, but with a class member, I get different results:</p>
  
  <pre><code>//  testmove2.cpp
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class MyClass {
      std::string m_data;
  
      public:
          MyClass(const std::string&amp; data) :
              m_data(data) {}
          MyClass(const MyClass&amp;&amp; other) :
              m_data{std::move(other.m_data)} {};
          const std::string&amp; get_data() const { return m_data; }
  };
  
  int main() {
      MyClass c1{"Object"};
      std::cout &lt;&lt; "c1: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      MyClass c2{std::move(c1)};
      std::cout &lt;&lt; "c2: [" &lt;&lt; c2.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      std::cout &lt;&lt; "c1 after move: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>with output:</p>
  
  <pre><code>paul@local:~$ ./testmove2
  c1: [Object]
  c2: [Object]
  c1 after move: [Object]
  paul@local:~$ 
  </code></pre>
  
  <p><em>Not</em> having the second string cleared out also seems like a feasible alternative, so this is not in itself surprising. It is surprising to me that this behavior is different purely as a result of putting the string in a class, however. Valgrind also reports a single allocation being made here.</p>
  
  <p>To test whether they're actually pointing to the same thing, I can change <code>c2</code> after the move, and check whether <code>c1</code> is also getting changed:</p>
  
  <pre><code>//  testmove3.cpp
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class MyClass {
      std::string m_data;
  
      public:
          MyClass(const std::string&amp; data) :
              m_data(data) {}
          MyClass(const MyClass&amp;&amp; other) :
              m_data{std::move(other.m_data)} {};
          const std::string&amp; get_data() const { return m_data; }
          void change_data() { m_data[0] = 'A'; }
  };
  
  int main() {
      MyClass c1{"Object"};
      std::cout &lt;&lt; "c1: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      MyClass c2{std::move(c1)};
      std::cout &lt;&lt; "c2: [" &lt;&lt; c2.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      std::cout &lt;&lt; "c1 after move: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
  
      c2.change_data();
      std::cout &lt;&lt; "c1 after change: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      std::cout &lt;&lt; "c2 after change: [" &lt;&lt; c2.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>paul@local:~$ ./testmove3
  c1: [Object]
  c2: [Object]
  c1 after move: [Object]
  c1 after change: [Object]
  c2 after change: [Abject]
  paul@local:~$ 
  </code></pre>
  
  <p>Here, the two objects are clearly not pointing to the same thing, as changing <code>c2</code> does not affect what's stored in <code>c1</code>. Valgrind now reports 2 allocations, which seems obviously necessary to explain the observed behavior as we clearly have two different strings, but it's not obvious to me why we suddenly get 2 allocations purely as a result of changing one of them. If I get rid of the move altogether, and just create <code>c1</code> and then call <code>change_data()</code> on it, I only get 1 allocation as you'd expect.</p>
  
  <p>We can get rid of the undefined behavior (barring any other errors on my part) by removing all accesses to <code>c1</code> after the move:</p>
  
  <pre><code>// testmove4.cpp
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class MyClass {
      std::string m_data;
  
      public:
          MyClass(const std::string&amp; data) :
              m_data(data) {}
          MyClass(const MyClass&amp;&amp; other) :
              m_data{std::move(other.m_data)} {};
          const std::string&amp; get_data() const { return m_data; }
          void change_data() { m_data[0] = 'A'; }
  };
  
  int main() {
      MyClass c1{"Object"};
      std::cout &lt;&lt; "c1: [" &lt;&lt; c1.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
      MyClass c2{std::move(c1)};
      std::cout &lt;&lt; "c2: [" &lt;&lt; c2.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
  
      c2.change_data();
      std::cout &lt;&lt; "c2 after change: [" &lt;&lt; c2.get_data() &lt;&lt; "]" &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  
  <p>which outputs:</p>
  
  <pre><code>paul@local:~$ ./testmove4
  c1: [Object]
  c2: [Object]
  c2 after change: [Abject]
  paul@local:~$ 
  </code></pre>
  
  <p>We can obviously no longer see the fact that <code>c1</code> is not changing, since we're not outputting it. But Valgrind still shows 2 allocations. </p>
  
  <p>Anyone know what's going on, here?</p>
  
  <ol>
  <li><p>Why does a <code>std::string</code> appear to be zeroed out following a move when it's by itself, but not when it's a class member?</p></li>
  <li><p>In the final example, when I move an object and then change it, why I am getting two allocations rather than one, when I only get one allocation when I move the object and then don't change it? I know we seem to be heading towards quantum computing, but having the Uncertainty Principle at play in C++ seems a little premature.</p></li>
  </ol>
  
  <p>I'm using g++ 4.7.2, but I get the same observed behavior on clang-503.0.40.</p>
  
  <p>EDIT: Thinking it over, if you end up with two objects in valid states, it does make sense for them both to have allocations. Is this just the compiler optimizing away one of those allocations when it can ascertain that one of them will never be used? An annoying hazard of constructing a minimal example, if it is.</p>