1
class test {
public:
    test() = default;
    ~test() = default;
    void recreate() {
        this->~test();
        new (this) test{};
    }
};

void main() {
    test b;
    b.recreate();
}

Leaving aside your questions like "why do you need this", my question - is this valid and acceptable C++ code? No hidden UB here?

It compiles in MSVC 2017.

Starl1ght
  • 4,422
  • 1
  • 21
  • 49
  • @Snef Not a dupe, in my opinion, as there is no assignment going on. I'm sure it _is_ a dupe, just not of that one. –  Apr 22 '17 at 16:25
  • For a start `void main` should be `int main`. – Ed Heal Apr 22 '17 at 16:25
  • Perhaps related: https://stackoverflow.com/questions/8829548/destroy-and-then-construct-new-object-using-the-same-variable – Kerrek SB Apr 22 '17 at 16:27

0 Answers0