title: Is it valid to call destructor on 'this' and then assign a new value to 'this'?
body: <p>Suppose I'm implementing some class <code>A</code> that has a <code>clear()</code> method that should set the object state to the &quot;brand new&quot; state as if it has just been created with a constructor:</p>
  <ul>
  <li>I should free all the resources that the current object is using (exactly the same thing <code>A::~A()</code> does),</li>
  <li>and then I should initialize those resources again (exactly the same thing <code>A::A()</code> does).</li>
  </ul>
  <p>So my initial idea was as follows:</p>
  <pre><code>void A::clear() {
    this-&gt;~A();
    *this = A();
  }
  </code></pre>
  <p>However, I was told that this code causes UB since I cannot dereference <code>this</code> after calling its destructor. But at the same time I was told a better idea as well: if we use placement new, there is no dereferencing, so this actually might work:</p>
  <pre><code>void A::clear() {
    this-&gt;~A();
    new (this) A();
  }
  </code></pre>
  <p>This feels extremely uncomfortable and as error-prone as it gets... So is this code actually valid?</p>