Does a call to a trivial destructor end the lifetime of an object?
I read this and this but didn't find a good explanation. These threads state that a trivial destructor call has no effect and code like struct A { int x; } a; a.~A(); a.~A();
is legal.
But I found that example in the standard:
struct C { };
void f() {
C * pc = new C;
using C2 = C;
pc->C::~C2(); // OK, destroys *pc
C().C::~C(); // undefined behavior: temporary of type C destroyed twice
using T = int;
0 .T::~T(); // OK, no effect
0.T::~T(); // error: 0.T is a user-defined-floating-point-literal (5.13.8)
}
Here C has trivial destructor but still double destruction of an object of type C has undefined behavior?
0.T
even if you don't provideoperator ""T
. – PeacheyHowever, if a program ends the lifetime of an non-trivial object explicitly, it must ensure that a new object of the same type is constructed in-place (e.g. via placement new) before the destructor may be called implicitly...
But what if the object is trivial? You say it's undefined, but I don't find anything on cppreference. – Rewardinglanguage-lawyer
tag, although it looks to me like walnut already addressed the crux of the question. – Mercurio