删除析构函数的意义是什么? [英] What is the point of deleted destructor?
问题描述
我遇到规则( N3797 :: 12.8 / 11 [class.copy]
)
隐式声明的复制/移动构造函数是其类的内联public
成员。如果X具有:
An implicitly-declared copy/move constructor is an inline public member of its class. A defaulted copy/ move constructor for a class X is defined as deleted (8.4.3) if X has:
[...]
- 类型为
的任何直接或虚拟基类或非静态数据成员,具有的析构函数被删除或无法从默认
构造函数或
— any direct or virtual base class or non-static data member of a type with a destructor that is deleted or inaccessible from the defaulted constructor, or
[...]
但是我不能得到被删除的析构函数出现在虚拟或直接基类中的点。考虑下面的简单例子:
But I can't get the point of deleted destructor appearing in a virtual or direct base class at all. Consider the following simple example:
struct A
{
~A() = delete;
A(){ }
};
struct B : A
{
B(){ }; //error: use of deleted function 'A::~A()'
};
B b;
int main() { }
DEMO
这完全不清楚对我来说。我明确定义了0参数构造函数,它不使用基类析构函数。但编译器认为不是。即使我们明确定义 B
的析构函数,它也不会工作:
It's perfectly unclear to me. I defined 0-argument constructor explcitly and it doesn't use base class destructor. But compiler thinks otherwise. It won't work even if we define B
's destructor explicitly:
struct A
{
~A() = delete;
A(){ }
};
struct B : A
{
B(){ };
~B(){ };
};
//B b;
int main() {
}
< DEMO
DEMO
推荐答案
这个项目符号的理由在缺陷报告1191:删除子对象析构函数和隐式定义的构造函数
The rationale for that bullet is covered in defect report 1191: Deleted subobject destructors and implicitly-defined constructors which says:
请考虑以下示例:
Consider the following example:
struct A {
A();
~A() = delete;
};
struct B: A { };
B* b = new B;
根据当前规则,B()不会被删除, b如果在
完成A的构造之后通过异常退出,则调用删除的〜A :: A()。删除的子对象析构函数
应该被添加到隐式删除的原因列表中在12.1
[class.ctor]和12.8 [class.copy]中。
Under the current rules, B() is not deleted, but is ill-formed because it calls the deleted ~A::A() if it exits via an exception after the completion of the construction of A. A deleted subobject destructor should be added to the list of reasons for implicit deletion in 12.1 [class.ctor] and 12.8 [class.copy].
bullet你注意上面和同样的措辞到 12.1
[class.ctor]第5段:
and the proposed resolution was to add the bullet you note above and the same wording to the following section 12.1
[class.ctor] paragraph 5:
任何直接或虚拟基类或非静态数据成员的类型具有从默认默认构造函数中删除或无法访问的析构函数。
any direct or virtual base class or non-static data member has a type with a destructor that is deleted or inaccessible from the defaulted default constructor.
这篇关于删除析构函数的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!