Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

c++ - What is the point of deleted destructor?

I come across the rule (section N3797::12.8/11 [class.copy])

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

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

Couldn't you clarify that thing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

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].

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...