OSDN Git Service

d5fd74bcf729ba7c8b269314991a3645e9a19b45
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.old-deja / g++.eh / new2.C
1 // { dg-do run  }
2 // Test that a throw in B's constructor destroys the A and frees the memory.
3
4 #include <cstddef>
5 #include <cstdlib>
6 #include <new>
7
8 struct A {
9   A();
10   ~A();
11 };
12
13 struct B {
14   B (A);
15 };
16
17 void foo (B*);
18
19 int newed, created;
20
21 int main ()
22 {
23   newed = 0; // The libraries might call new before main starts.
24   try {
25     foo (new B (A ()));
26   } catch (...) { }
27
28   return !(!newed && !created);
29 }
30
31 A::A() { created = 1; }
32 A::~A() { created = 0; }
33 B::B(A) { throw 1; }
34 void foo (B*) { }
35
36 void* operator new (size_t size)
37 #if __cplusplus <= 199711L
38   throw (std::bad_alloc)
39 #endif
40 {
41   ++newed;
42   return (void *) std::malloc (size);
43 }
44
45 void operator delete (void *p) throw ()
46 {
47   --newed;
48   free (p);
49 }
50