for better understand this problem, let's see the code1:
// code1
struct A {
int e;
};
struct B : A {
virtual ~B() { }
};
struct C : B {
~C() { }
};
as you can see, class A is trivially destructible
Q1: is the behavor is undefined if we destory the instance of the class C through the pointer of the class A in code1?
Q2: is the behavor is undefined if we destory the instance of the class C through the pointer of the class B in code1?
here is the another version, call it code2:
//code2
struct A {
int e;
~A() { }
};
struct B : A {
virtual ~B() { }
};
struct C : B {
~C() { }
};
for now, class A has a user-provided destructor. so the class A is not trivially destructible
Q3: is the behavor is undefined if we destory the instance of the class C through the pointer of the class A in code2?
Q4: is the behavor is undefined if we destory the instance of the class C through the pointer of the class B in code2?
I have read the standard ISO/IEC 14882:2011(E) [expr.delete]/3
In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.
Q5: the standard said the static type shall have a virtual destructor, but what about the base class of that static type?