131ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh// { dg-do run }
231ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
331ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehstruct ex;
431ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehstruct basic {
531ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  int refcount;
631ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  ex eval() const;
731ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  basic() : refcount(0) {}
831ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh};
931ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
1031ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehstruct ex {
1131ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  basic *bp;
1231ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  ex() : bp(0) { }
1331ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  ex(const basic &);
1431ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  virtual ~ex();
1531ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  void construct_from_basic(const basic &);
1631ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh};
1731ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
1831ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehex basic::eval() const {
1931ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  throw 1;
2031ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh}
2131ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
2231ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehinline ex::ex(const basic &b) { construct_from_basic (b); }
2331ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehinline ex::~ex() { if (--bp->refcount == 0) delete bp; }
2431ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehvoid ex::construct_from_basic(const basic &b) {
2531ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  const ex & tmpex = b.eval();
2631ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  bp = tmpex.bp;
2731ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  bp->refcount++;
2831ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh}
2931ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
3031ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehex pow() { return basic(); }
3131ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh
3231ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsiehint main()
3331ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh{
3431ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  try { pow (); } catch (int) {}
3531ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh  return 0;
3631ad4de047c50cddfc3072be3a82232c53f1ca2dAndrew Hsieh}
37