c++: Add testcase for DR 2874
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.martin / new1.C
blob68a5fd8549160f57b274d4c0089075d2bb56ad41
1 // { dg-do run  }
2 // { dg-skip-if "requires hosted libstdc++ for cstdio" { ! hostedlib } }
3 //Lifetime of temporaries:
4 //egcs 2.92 performs cleanup for temporaries inside new expressions
5 //after the new is complete, not at the end of the full expression.
7 #include <new>
8 #include <cstdlib>
9 #include <cstdio>
11 bool new_throws;
12 bool ctor_throws;
14 int new_done;
15 int ctor_done;
16 int func_done;
17 int dtor_done;
18 int delete_done;
20 int count;
22 void init()
24   new_throws = ctor_throws = false;
25   new_done = ctor_done = func_done = dtor_done = delete_done = count = 0;
28 struct line_error{
29   int line;
30   line_error(int i):line(i){}
33 #define CHECK(cond)  if(!(cond))throw line_error(__LINE__);
35 struct A{
36   A(int){
37     ctor_done = ++count;
38     if(ctor_throws)
39       throw 1;
40   }
41   A(const A&){
42     CHECK(false); //no copy constructors in this code
43   }
44   ~A(){
45     dtor_done = ++count;
46   }
47   A* addr(){return this;}
50 struct B{
51   B(A*){}
52   void* operator new(size_t s){
53     new_done = ++count;
54     if(new_throws)
55       throw 1;
56     return malloc(s);
57   }
58   void operator delete(void *){
59     delete_done = ++count;
60   }
63 void func(B* )
65   func_done = ++count;
68 void test1()
70   init();
71   try{
72     func(new B(A(10).addr()));
73   }catch(int){
74   }
75   CHECK(new_done==1);
76   CHECK(ctor_done==2);
77   CHECK(func_done==3);
78   CHECK(dtor_done==4);
79   CHECK(delete_done==0);
82 void test2()
84   init();
85   new_throws = true;
86   try{
87     func(new B(A(10).addr()));
88   }catch(int){
89   }
90   CHECK(new_done==1);
91   CHECK(ctor_done==0);
92   CHECK(func_done==0);
93   CHECK(dtor_done==0);
94   CHECK(delete_done==0);
97 void test3()
99   init();
100   ctor_throws = true;
101   try{
102     func(new B(A(10).addr()));
103   }catch(int){
104   }
105   CHECK(new_done==1);
106   CHECK(ctor_done==2);
107   CHECK(func_done==0);
108   CHECK(dtor_done==0);
109   CHECK(delete_done==3);
112 int main()
114   try{
115     test1();
116     test2();
117     test3();
118   }catch(line_error e){
119     printf("Got error in line %d\n",e.line);
120     return 1;
121   }
122   return 0;