在这里显式调用析构函数是否导致未定义的行为? [英] Does explicitly calling destructor result in Undefined Behavior here?

查看:248
本文介绍了在这里显式调用析构函数是否导致未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,下面的代码(从一些C ++问题)应该导致UB,但它似乎不是。这里是代码:

In my opinion, the following code (from some C++ question) should lead to UB, but the it seems it is not. Here is the code:

#include <iostream>
using namespace std;
class some{ public: ~some() { cout<<"some's destructor"<<endl; } };
int main() { some s; s.~some(); }

,答案是:

some's destructor
some's destructor

faq lite我们不应该显式调用析构函数。我认为在显式调用析构函数后,对象s应该被删除。程序在完成后自动调用析构函数,它应该是UB。

I learned form c++ faq lite that we should not explicitly call destructor. I think after the explicitly call to the destructor, the object s should be deleted. The program automatically calls the destructor again when it's finished, it should be UB. However, I tried it on g++, and get the same result as the above answer.

这是因为类太简单了(没有新的/删除涉及)?

Is it because the class is too simple (no new/delete involved)? Or it's not UB at all in this case?

推荐答案

行为是未定义的,因为析构函数对同一个对象被调用了两次:

The behavior is undefined because the destructor is invoked twice for the same object:


  • 一旦您明确调用它

  • 一旦范围结束,自动变量被销毁

对生命周期结束的对象调用析构函数会导致每个C ++ 03的行为不确定§§1.4.4 >

Invoking the destructor on an object whose lifetime has ended results in undefined behavior per C++03 §12.4/6:


如果为生命周期已结束的对象调用析构函数,行为未定义

the behavior is undefined if the destructor is invoked for an object whose lifetime has ended

当根据§3.8/ 1调用析构函数时,对象的生命周期结束:

An object's lifetime ends when its destructor is called per §3.8/1:


对象的生命周期 T 结束于

是具有非平凡析构函数(12.4)的类类型,析构函数调用开始,或

— if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

- 对象占用的存储被重用或释放。 >

— the storage which the object occupies is reused or released.

注意,这意味着如果你的类有一个简单的析构函数,行为是很好定义的,因为这种类型的对象的生命周期不结束直到它的存储被释放,这对于自动变量不发生直到函数结束。当然,我不知道为什么你将显式调用析构函数,如果它是微不足道的。

Note that this means if your class has a trivial destructor, the behavior is well-defined because the lifetime of an object of such a type does not end until its storage is released, which for automatic variables does not happen until the end of the function. Of course, I don't know why you would explicitly invoke the destructor if it is trivial.

什么是小型析构函数? §12.4/ 3说:

What is a trivial destructor? §12.4/3 says:


析构函数是一个隐式声明的析构函数,如果:

A destructor is trivial if it is an implicitly-declared destructor and if:

- 其类的所有直接基类都有简单的析构函数和

— all of the direct base classes of its class have trivial destructors and

- 对其类的所有非静态数据成员

— for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

正如其他人所提到的,一个可能的结果的未定义的行为是你的程序似乎继续正确运行;另一个可能的结果是你的程序崩溃。任何事情都可能发生,并且没有任何保证。

As others have mentioned, one possible result of undefined behavior is your program appearing to continue running correctly; another possible result is your program crashing. Anything can happen and there are no guarantees whatsoever.

这篇关于在这里显式调用析构函数是否导致未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆