V
vgrebinski
Hi everyone,
Suppose we have a diamond shaped diagram:
A
/ \
B1 B2
\ /
C
and assume that C' constructor invokes
A, B1, B2, C
C's destructors then are called in the reverse:
C, B2, B1, A
What is SUPPOSED to hapen if ~B2() throws an exception?
I would think, that ~B1() and ~A() should be called to finish the
destruction of C (just like in case if we were constructing C and there
was an exception in B2's constructor), however, some popular compilers
give different results (below is the sequence of destructors called to
destroy C with an exception thrown in B2):
gcc-3.3 and up: C, B2, B1, A // seems to be OK
Intel icc9: C, B2, A, B1 , A // ~A() called twice!
MS VC6/2005Express: C, B2, B1 // where is ~A() ?
So, what standard says about this situation? Is it undefined or gcc is
right?
Regards,
Vladimir
Below is a short program to demonstrate the behavior:
///////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
struct A {~A() {cout << "A" <<endl;}};
struct B1 : public virtual A {~B1() {cout << "B1" <<endl;}};
struct B2 : public virtual A {
~B2() {
cout << "B2" <<endl;
throw "bad things happened";
}
};
struct C : public B1, public B2 {~C() {cout << "C" <<endl;}};
int main(int argc, char* argv[])
{
try {
C * p = new C;
delete p;
} catch (...) {};
return 0;
}
//////////////////////////////////
Suppose we have a diamond shaped diagram:
A
/ \
B1 B2
\ /
C
and assume that C' constructor invokes
A, B1, B2, C
C's destructors then are called in the reverse:
C, B2, B1, A
What is SUPPOSED to hapen if ~B2() throws an exception?
I would think, that ~B1() and ~A() should be called to finish the
destruction of C (just like in case if we were constructing C and there
was an exception in B2's constructor), however, some popular compilers
give different results (below is the sequence of destructors called to
destroy C with an exception thrown in B2):
gcc-3.3 and up: C, B2, B1, A // seems to be OK
Intel icc9: C, B2, A, B1 , A // ~A() called twice!
MS VC6/2005Express: C, B2, B1 // where is ~A() ?
So, what standard says about this situation? Is it undefined or gcc is
right?
Regards,
Vladimir
Below is a short program to demonstrate the behavior:
///////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
struct A {~A() {cout << "A" <<endl;}};
struct B1 : public virtual A {~B1() {cout << "B1" <<endl;}};
struct B2 : public virtual A {
~B2() {
cout << "B2" <<endl;
throw "bad things happened";
}
};
struct C : public B1, public B2 {~C() {cout << "C" <<endl;}};
int main(int argc, char* argv[])
{
try {
C * p = new C;
delete p;
} catch (...) {};
return 0;
}
//////////////////////////////////