1 // { dg-do run { xfail sparc64-*-elf arm-*-pe } } 2 // { dg-options "-fexceptions" } 3 // prms-id: 11667 4 5 extern "C" int printf(const char *,...); 6 7 template < class T > 8 class LIST { 9 public: 10 LIST()11 LIST() { nitems = 16; items = new T[nitems]; }; 12 LIST(int u)13 LIST(int u) { nitems = u; items = new T[nitems]; }; 14 15 T& operator[](int i) const { 16 return items[i]; 17 } 18 grow(int n)19 void grow(int n) { 20 T* newlist = new T[n]; 21 T* src = items; 22 T* dst = newlist; 23 int i = nitems; 24 25 try { 26 while (i--) *dst++ = *src++; 27 } catch (...) { 28 delete[] newlist; 29 throw; 30 } 31 32 if (items) delete[] items; 33 nitems = n; 34 items = newlist; 35 } 36 37 private: 38 int nitems; 39 T *items; 40 }; 41 main(int argc,char ** argv)42int main(int argc, char **argv) { 43 int i; 44 LIST<int> mylist(10); 45 46 printf("Start dumping initial 10 item list\n"); 47 for (i = 0; i < 10 ; i++) { 48 mylist[i] = i; 49 printf("%d\n", mylist[i]); 50 } 51 52 printf("Growing list to 20\n"); 53 mylist.grow(20); 54 55 printf("Start dumping grown 20 item list\n"); 56 for (i = 0; i < 20; i++) { 57 mylist[i] = i; 58 printf("%d\n", mylist[i]); 59 } 60 61 return 0; 62 } 63