5

Suppose I have a class like so:

#include <new>
struct thing {
  thing() = default;
  void foo()
  {
    this->~thing();
    new (this) thing();
  }
};

Is calling the destructor like this and then reconstructing the object using a placement new defined behavior?

DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • Nothing wrong with this, as long as there are no multiple execution threads involved. Another execution thread attempting to use the `thing` while it's destroyed is, of course, undefined behavior. – Sam Varshavchik Mar 17 '18 at 14:30
  • @liliscent I don't believe this is a duplicate of that question, because there might be some interplay between the fact that this happens in a member function and is operating on the "this" pointer as apposed to a normal function – DarthRubik Mar 17 '18 at 14:32
  • 1
    Resist the temptation. More info in [this SO post](https://stackoverflow.com/questions/12512961/does-destroying-and-recreating-an-object-make-all-pointers-to-this-object-invali). – Ron Mar 17 '18 at 14:34

0 Answers0