You can do that, but you'll need memory laundering for that if you have reference or const members, or if the type of the class changes.
Consider this:
struct FooView {
const int val;
void refresh()
{
this->~FooView();
new (this) FooView{5};
}
}
int main() {
FooView fv{9};
std::cout << fv.val; // surely 9!
fv.refresh();
std::cout << fv.val; // hmm... val is a const object, so it's 9 still?
}
To avoid this undefined behaviour, you should launder the memory using std::launder
. The compiler will assume that the lifetime of fv
won't be affected by anything except }
. Laundering will make the compiler assume there is an object, unrelated to fv
:
int main() {
FooView fv{9};
std::cout << fv.val; // surely 9!
fv.refresh();
std::cout << std::launder(&fv)->val; // yay, 5
}
Now is it a good idea? I'd advise against it since it can lead to confusion but it can be done safely.