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?
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.
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With