| 1 |
// PR c++/50866, adjusted |
| 2 |
// { dg-do run } |
| 3 |
|
| 4 |
#if __cplusplus > 201100L |
| 5 |
#define THROWING noexcept(false) |
| 6 |
#else |
| 7 |
#define THROWING |
| 8 |
#endif |
| 9 |
|
| 10 |
extern "C" void abort (); |
| 11 |
|
| 12 |
#ifdef DEBUG |
| 13 |
extern "C" int printf (const char *, ...); |
| 14 |
#define dump(X,Y) printf(X,Y) |
| 15 |
#define abort() printf("wrong\n"); |
| 16 |
|
| 17 |
#else |
| 18 |
#define dump(X,Y) |
| 19 |
#endif |
| 20 |
|
| 21 |
int a, b; |
| 22 |
int d; |
| 23 |
struct A { |
| 24 |
int n; |
| 25 |
A() { n = ++a; dump("A%d\n",a); } |
| 26 |
A(const A&); |
| 27 |
~A() THROWING { |
| 28 |
dump("~A%d\n",n); |
| 29 |
--a; |
| 30 |
if (d == 1 ? a == 0 : (b == d && a == 1)) |
| 31 |
{ |
| 32 |
dump ("~A%d throwing\n", n); |
| 33 |
throw (short)b; |
| 34 |
} |
| 35 |
} |
| 36 |
}; |
| 37 |
int t; |
| 38 |
struct B { |
| 39 |
int n; |
| 40 |
B(const A& = A()) |
| 41 |
{ |
| 42 |
if (b == t) |
| 43 |
{ |
| 44 |
dump ("B%d throwing\n", b+1); |
| 45 |
throw b; |
| 46 |
} |
| 47 |
n = ++b; |
| 48 |
dump("B%d\n",b); |
| 49 |
|
| 50 |
/* The first B has an explicit initializer, so its A lives for the |
| 51 |
full-expression. The second B does not, so its A should be destroyed |
| 52 |
before we construct the third B. */ |
| 53 |
if (a != 2) abort (); |
| 54 |
} |
| 55 |
B(const char *, const A& = A()) |
| 56 |
{ |
| 57 |
if (b == t) |
| 58 |
{ |
| 59 |
dump ("B%d throwing\n", b+1); |
| 60 |
throw b; |
| 61 |
} |
| 62 |
n = ++b; |
| 63 |
dump("B%d\n",b); |
| 64 |
if (a != b) abort (); |
| 65 |
} |
| 66 |
B(const B&); |
| 67 |
~B() |
| 68 |
{ |
| 69 |
dump("~B%d\n",n); |
| 70 |
--b; |
| 71 |
} |
| 72 |
}; |
| 73 |
struct C { |
| 74 |
B bs[3]; |
| 75 |
}; |
| 76 |
void f() |
| 77 |
{ |
| 78 |
a = b = 0; |
| 79 |
try |
| 80 |
{ |
| 81 |
C c = { "x" }; |
| 82 |
if (a != 0) abort (); |
| 83 |
if (b != 3) abort (); |
| 84 |
} |
| 85 |
catch (int i) { } |
| 86 |
catch (short s) { } |
| 87 |
if (a != 0) abort (); |
| 88 |
if (b != 0) abort (); |
| 89 |
dump ("\n", 0); |
| 90 |
} |
| 91 |
|
| 92 |
int main() |
| 93 |
{ |
| 94 |
for (t = 0; t <= 3; ++t) |
| 95 |
f(); |
| 96 |
for (d = 1; d <= 3; ++d) |
| 97 |
f(); |
| 98 |
} |