隐式使用析构函数
Implicit use of destructor
本文关键字:析构函数 更新时间:2023-10-16
我有一个类有一个已删除的析构函数(在实践中,它需要外部帮助才能被销毁):
struct indestructible {
indestructible(indestructible&&);
~indestructible() = delete;
};
当我尝试使用它的move构造函数时,编译器会抱怨:
struct user {
indestructible ind;
user(indestructible&& ind) : ind(std::move(ind)) {}
};
indestructible.cc:11:3: error: attempt to use a deleted function
user(indestructible&& ind) : ind(std::move(ind)) {}
^
indestructible.cc:6:3: note: '~indestructible' has been explicitly marked deleted here
~indestructible() = delete;
怎么回事?没有其他成员可以抛出,构造函数主体也不能抛出,那么为什么move构造函数会调用析构函数呢?
当user
对象超出范围时,它将被销毁。它的成员被破坏,包括indestructible
成员,这是不可能的,因为它的析构函数被删除了。
[class.base.init]/12:
在非委托构造函数中,每个直接或虚拟基类和类类型的每个非静态数据成员可能被调用(12.4)。[注意:此条款确保如果抛出异常(15.2)。--尾注]
[class.dtor]/11:
如果可能被调用的析构函数是已从调用的上下文中删除或无法访问。
对于不抛出的构造函数,没有任何例外。另见CWG 1915。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 优先顺序:智能指针和类析构函数
- C++-明确何时以及如何调用析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 在c++中使用向量时,如何调用构造函数和析构函数
- 重载运算符new[]的行为取决于析构函数
- 我需要知道编译器如何在cpp中使用析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 析构函数调用
- 通过引用传递-为什么要调用这个析构函数
- 对具有动态分配的内存和析构函数的类对象的引用
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- C++成员的析构函数顺序与shared_ptr
- C++ 防止在映射中放置()时调用析构函数
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 调用析构函数以释放动态分配的内存
- 不命名构造函数和析构函数上的类型错误