c++ - 失败的构造函数如何翻转以销毁已完成的对象?

标签 c++

我知道当构造函数失败时,已完成的成员对象将被销毁。没有内存泄漏。

我的问题是编译器是如何做到这一点的?编译器如何知道构造了什么成员?它有记录吗?在这种情况下,编译器真的会破坏所有内容吗?它如何保证这一点?

最佳答案

编译器如何做到这一点取决于编译器。

但是,是的,您可以保证任何构造的对象都将被破坏(以与构造它们相反的顺序)。 §15.2/2:

An object that is partially constructed or partially destroyed will have destructors executed for all of its fully constructed subobjects, that is, for subobjects for which the constructor has completed execution and the destructor has not yet begun execution. Should a constructor for an element of an automatic array throw an exception, only the constructed elements of that array will be destroyed. If the object or array was allocated in a new-expression, the matching deallocation function (3.7.3.2, 5.3.4, 12.5), if any, is called to free the storage occupied by the object.

关于c++ - 失败的构造函数如何翻转以销毁已完成的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2372291/

相关文章:

c++ - 这个实例中#define 的 Action 是什么?

关于特定编译器/平台问题的 C++ null-terminated-string

c++ - std::cout 不打印到控制台

c++ - 我在 C++ 中创建阻塞队列 vector 时遇到问题

c++ - Visual C++ 2013 - Windows XP Sp1 C 程序不工作 - 空白命令行

c++ - 基于变量的输出

c++ - 为什么 google test sample 将测试放在匿名命名空间中?

c++ - omp_get_max_threads() 在并行区域返回 1,但它应该是 8

c++ - 关于 'C++ Coding Standards' 书中的最佳实践和宏的问题

c++ - 为什么sv后缀引入的字符串不会过期?