FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.martin / new1.C
blob9e37be8d154acb6966ef61688f41963e2740d92f
1 //Lifetime of temporaries: 
2 //egcs 2.92 performs cleanup for temporaries inside new expressions
3 //after the new is complete, not at the end of the full expression.
5 #include <new>
6 #include <cstdlib>
7 #include <cstdio>
9 bool new_throws;
10 bool ctor_throws;
12 int new_done;
13 int ctor_done;
14 int func_done;
15 int dtor_done;
16 int delete_done;
18 int count;
20 void init()
22   new_throws = ctor_throws = false;
23   new_done = ctor_done = func_done = dtor_done = delete_done = count = 0;
26 struct line_error{
27   int line;
28   line_error(int i):line(i){}
31 #define CHECK(cond)  if(!(cond))throw line_error(__LINE__);
33 struct A{
34   A(int){
35     ctor_done = ++count;
36     if(ctor_throws)
37       throw 1;
38   }
39   A(const A&){
40     CHECK(false); //no copy constructors in this code
41   }
42   ~A(){
43     dtor_done = ++count;
44   }
45   A* addr(){return this;}
48 struct B{
49   B(A*){}
50   void* operator new(size_t s){
51     new_done = ++count;
52     if(new_throws)
53       throw 1;
54     return malloc(s);
55   }
56   void operator delete(void *){
57     delete_done = ++count;
58   }
61 void func(B* )
63   func_done = ++count;
66 void test1()
68   init();
69   try{
70     func(new B(A(10).addr()));
71   }catch(int){
72   }
73   CHECK(new_done==1);
74   CHECK(ctor_done==2);
75   CHECK(func_done==3);
76   CHECK(dtor_done==4);
77   CHECK(delete_done==0);
80 void test2()
82   init();
83   new_throws = true;
84   try{
85     func(new B(A(10).addr()));
86   }catch(int){
87   }
88   CHECK(new_done==1);
89   CHECK(ctor_done==0);
90   CHECK(func_done==0);
91   CHECK(dtor_done==0);
92   CHECK(delete_done==0);
95 void test3()
97   init();
98   ctor_throws = true;
99   try{
100     func(new B(A(10).addr()));
101   }catch(int){
102   }
103   CHECK(new_done==1);
104   CHECK(ctor_done==2);
105   CHECK(func_done==0);
106   CHECK(dtor_done==0);
107   CHECK(delete_done==3);
110 int main()
112   try{
113     test1();
114     test2();
115     test3();
116   }catch(line_error e){
117     printf("Got error in line %d\n",e.line);
118     return 1;
119   }
120   return 0;