title: Why synthesized copy-assignment operator is defined as deleted if the class has a reference member?
body: <p>In C++ Primer, Fifth Edition, §13.1.6:</p>
  
  <blockquote>
    <p>The synthesized copy-assignment operator is defined as deleted if a member has a deleted or inaccessible copy-assignment operator, or if the class has a const or reference member.</p>
  </blockquote>
  
  <p>The explanation from the chapter:</p>
  
  <blockquote>
    <p>Although we can assign a new value to a reference, doing so changes the value of the object to which the reference refers. If the copy-assignment operator were synthesized for such classes, the left-hand operand would continue to refer to the same object as it did before the assignment. It would not refer to the same object as the right-hand operand. Because this behavior <em>is unlikely to be desired</em>, the synthesized copy-assignment operator is defined as deleted if the class has a reference member.</p>
  </blockquote>
  
  <p>Copying the class changes the object to which the reference member refers. Isn't this desired? Why the explanation say "<em>is unlikely to be desired</em>"?</p>
  
  <p>Concretely,</p>
  
  <pre><code>class A {
  public:
      A(int &amp;n) : a(n) {}
  private:
      int &amp;a;
  };
  
  int main() {
      int n = 1;
  
      A a(n);
  
      /* Why is this allowed? */
      A b(a);
  
      /*
      Why is this not allowed?
      error C2280: 'A &amp;A::operator =(const A &amp;)': attempting to reference a deleted function
      */
      b = a;
  
      return 0;
  }
  </code></pre>