问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/26942887/what-is-the-point-of-deleted-destructor