1 #include <stdio.h> 2 3 template <typename V> struct S 4 { 5 V *f, *l; SS6 __attribute__ ((noinline)) S (void) { f = 0, l = 0; } fooS7 void foo (V *x) 8 { 9 if (x->p != 0) 10 x->p->n = x->n; 11 else 12 f = x->n; 13 if (x->n != 0) 14 x->n->p = x->p; 15 else 16 l = x->p; 17 } barS18 __attribute__ ((noinline)) void bar (V *x) 19 { 20 x->n = 0; 21 x->p = l; 22 if (l != 0) 23 l->n = x; 24 else 25 f = x; 26 l = x; 27 } 28 }; 29 30 struct H; 31 32 struct A 33 { 34 S <H> k; 35 }; 36 37 struct H 38 { 39 A *a; 40 H *p, *n; HH41 __attribute__ ((noinline)) H (void) { p = 0, n = 0, a = 0; } HH42 __attribute__ ((noinline)) H (A *b) : a (b) 43 { 44 p = 0; 45 n = 0; 46 if (a != 0) 47 a->k.bar (this); 48 } HH49 __attribute__ ((noinline)) H (const H &h) : a (h.a) 50 { 51 p = 0; 52 n = 0; 53 if (a != 0) 54 a->k.bar (this); 55 } ~HH56 ~H (void) { if (a != 0) a->k.foo (this); } operator =H57 H &operator= (const H &o) 58 { 59 if (a != 0 || &o == this) 60 __builtin_abort (); 61 a = o.a; 62 if (a != 0) 63 a->k.bar (this); 64 return *this; 65 } 66 }; 67 68 __attribute__ ((noinline)) baz(void)69H baz (void) 70 { 71 return H (new A); 72 } 73 74 H g; 75 76 int main(void)77main (void) 78 { 79 g = baz (); 80 if (g.a->k.f != &g) 81 __builtin_abort (); 82 printf ("OK\n"); 83 return 0; 84 } 85 86