【发布时间】:2020-07-18 21:35:58
【问题描述】:
调用普通析构函数会结束对象的生命周期吗?
我阅读了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 类型的对象的双重破坏仍然有未定义的行为?
【问题讨论】:
-
除非你使用了placement new,否则你不应该调用析构函数。
-
你问的很好。未定义行为的有效影响之一是什么都没有发生,因此您必须保持警惕。 Does this link clear anything up?
-
0.T的“解析问题”即使您不提供operator ""T。 -
重读问题再重读链接,没有链接也没用。
-
@user4581301 仍然没有回答我的问题。
However, 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...但是如果对象是微不足道的呢?你说它是未定义的,但我在 cppreference 上没有找到任何东西。
标签: c++ language-lawyer destructor lifetime