1/* { dg-do run } */
2
3template <typename V> struct S
4{
5  V *f, *l;
6  __attribute__ ((noinline)) S (void) { f = 0, l = 0; }
7  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  }
18  __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
30struct H;
31
32struct A
33{
34  S <H> k;
35};
36
37struct H
38{
39  A *a;
40  H *p, *n;
41  __attribute__ ((noinline)) H (void) { p = 0, n = 0, a = 0; }
42  __attribute__ ((noinline)) H (A *b) : a (b)
43  {
44    p = 0;
45    n = 0;
46    if (a != 0)
47      a->k.bar (this);
48  }
49  __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  }
56  ~H (void) { if (a != 0) a->k.foo (this); }
57  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))
69H baz (void)
70{
71  return H (new A);
72}
73
74H g;
75
76int
77main (void)
78{
79  g = baz ();
80  if (g.a->k.f != &g)
81    __builtin_abort ();
82  return 0;
83}
84
85