title: const member and assignment operator. How to avoid the undefined behavior?
body: <p>I <a href="https://stackoverflow.com/questions/4124271/stdvector-of-objects-and-const-correctness/4125170#4125170">answered</a> the question about <a href="https://stackoverflow.com/questions/4124271/stdvector-of-objects-and-const-correctness/">std::vector of objects and const-correctness</a>, and received a comment about undefined behavior. I do not agree and therefore I have a question.</p>
  <p>Consider the class with const member:</p>
  <pre><code>class A { 
  public: 
      const int c; // must not be modified! 
      A(int c) : c(c) {} 
      A(const A&amp; copy) : c(copy.c) { }     
      // No assignment operator
  }; 
  </code></pre>
  <p>I want to have an assignment operator but I do not want to use <code>const_cast</code> like in the following code from one of the answers:</p>
  <pre><code>A&amp; operator=(const A&amp; assign) 
  { 
      *const_cast&lt;int*&gt; (&amp;c)= assign.c;  // very very bad, IMHO, it is undefined behavior
      return *this; 
  } 
  </code></pre>
  <p>My solution is</p>
  <pre><code>// Custom-defined assignment operator
  A&amp; operator=(const A&amp; right)  
  {  
      if (this == &amp;right) return *this;  
  
      // manually call the destructor of the old left-side object
      // (`this`) in the assignment operation to clean it up
      this-&gt;~A(); 
      // use &quot;placement new&quot; syntax to copy-construct a new `A` 
      // object from `right` into left (at address `this`)
      new (this) A(right); 
      return *this;  
  }  
  </code></pre>
  <p><strong>Do I have undefined behavior (UB)?</strong></p>
  <p>What would be a solution without UB?</p>