对琐碎析构函数的调用是否会结束对象的生命周期?我读了this和this,但找不到很好的解释。这些线程声明琐碎的析构函数调用无效,并且类似struct A { int x; } a; a.~A(); a.~A();
的代码合法。但我在标准中找到了该示例:
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)
}
C在这里具有微不足道的析构函数,但对C类型的对象进行两次销毁仍然具有未定义的行为吗?
从C ++ 20琐碎的析构函数调用开始,结束了对象的生命周期。在此之前,它们没有,并且多次调用析构函数是有效的。
这在C ++ 20草案(N4861)的(参考)[diff.cpp17.basic]/1节中作为重大更改进行了详细说明。