In this question of mine, @DeadMG says that reinitializing a class through the this
pointer is undefined behaviour. Is there any mentioning thereof in the standard somewhere?
Example:
#include <iostream>
class X{
int _i;
public:
X() : _i(0) { std::cout << "X()
"; }
X(int i) : _i(i) { std::cout << "X(int)
"; }
~X(){ std::cout << "~X()
"; }
void foo(){
this->~X();
new (this) X(5);
}
void print_i(){
std::cout << _i << "
";
}
};
int main(){
X x;
x.foo();
// mock random stack noise
int noise[20];
x.print_i();
}
Example output at Ideone (I know that UB can also be "seemingly correct behaviour").
Note that I did not call the destructor outside of the class, as to not access an object whose lifetime has ended. Also note, that @DeadMG says that directly calling the destructor is okay as-long-as it's called once for every constructor.