Is reference to object still valid after its destruction and recreation in-place?

Viewed 47

what will happen to the reference to an object after destruction and creation in-place of that object? Will it be still valid or it is undefined behavior? Here is an example:

#include <iostream>

class A
{
    int var;

public:
    A(int var) : var(var) {}

    void foo()
    {
        std::cout << var;
    }
};

A obj(5);

void main()
{
    A& ref = obj;
    obj.~A();
    new (&obj) A(6);
    
    ref.foo();  // is this valid?
}
0 Answers
Related