Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit use of destructor

Tags:

c++

c++11

I have a class with a deleted destructor (in practice, it needs outside help to be destroyed):

struct indestructible {
    indestructible(indestructible&&);
    ~indestructible() = delete;
};

When I try to use its move constructor, the compiler complains:

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;

What's going on? there are no other members that could throw, and neither does the constructor body, so why is there any cause for the move constructor to invoke the destructor?

like image 385
Avi Kivity Avatar asked Nov 03 '15 11:11

Avi Kivity


2 Answers

When your user object gets out of scope, it is destructed. Its members are destructed, including the indestructible member, and that's impossible since its destructor is deleted.

like image 101
JohnB Avatar answered Sep 20 '22 12:09

JohnB


[class.base.init]/12:

In a non-delegating constructor, the destructor for each direct or virtual base class and for each non-static data member of class type is potentially invoked (12.4). [ Note: This provision ensures that destructors can be called for fully-constructed sub-objects in case an exception is thrown (15.2). —end note ]

[class.dtor]/11:

A program is ill-formed if a destructor that is potentially invoked is deleted or not accessible from the context of the invocation.

No exception is made for a constructor that doesn't throw. See also CWG 1915.

like image 20
T.C. Avatar answered Sep 19 '22 12:09

T.C.