Home | History | Annotate | Download | only in StTerm-order
      1 /*
      2  * The conversation with Matti Rintala on STLport forum 2005-08-24:
      3  *
      4  * Do you mean ISO/IEC 14882 3.6.3 [basic.start.term]?
      5  *
      6  * Yes. "Destructors (12.4) for initialized objects of static storage duration
      7  * (declared at block scope or at namespace scope) are called as a result
      8  * of returning from main and as a result of calling exit (18.3). These objects
      9  * are destroyed in the reverse order of the completion of their constructor
     10  * or of the completion of their dynamic initialization."
     11  *
     12  * I found a confirmation on the web that gcc may not strictly conform
     13  * to this behaviour in certains cases unless -fuse-cxa-atexit is used.
     14  *
     15  * Test below give (without -fuse-cxa-atexit)
     16 
     17 Init::Init()
     18 Init::use_it
     19 It ctor done    <-- 0
     20 Init::use_it done
     21 Init ctor done  <-- 1
     22 Init2 ctor done <-- 2
     23 It dtor done    <-- 0
     24 Init2 dtor done <-- 2
     25 Init dtor done  <-- 1
     26 
     27 
     28  * but should:
     29 
     30 Init::Init()
     31 Init::use_it
     32 It ctor done    <-- 0
     33 Init::use_it done
     34 Init ctor done  <-- 1
     35 Init2 ctor done <-- 2
     36 Init2 dtor done <-- 2
     37 Init dtor done  <-- 1
     38 It dtor done    <-- 0
     39 
     40 
     41  */
     42 #include <stdio.h>
     43 
     44 using namespace std;
     45 
     46 class Init
     47 {
     48   public:
     49     Init();
     50     ~Init();
     51 
     52     static void use_it();
     53 };
     54 
     55 class Init2
     56 {
     57   public:
     58     Init2();
     59     ~Init2();
     60 
     61 };
     62 
     63 static Init init;
     64 static Init2 init2;
     65 
     66 class It
     67 {
     68   public:
     69     It();
     70     ~It();
     71 };
     72 
     73 Init::Init()
     74 {
     75   printf( "Init::Init()\n" );
     76   use_it();
     77   printf( "Init ctor done\n" );
     78 }
     79 
     80 Init::~Init()
     81 {
     82   printf( "Init dtor done\n" );
     83 }
     84 
     85 void Init::use_it()
     86 {
     87   printf( "Init::use_it\n" );
     88 
     89   static It it;
     90 
     91   printf( "Init::use_it done\n" );
     92 }
     93 
     94 Init2::Init2()
     95 {
     96   printf( "Init2 ctor done\n" );
     97 }
     98 
     99 Init2::~Init2()
    100 {
    101   printf( "Init2 dtor done\n" );
    102 }
    103 
    104 It::It()
    105 {
    106   printf( "It ctor done\n" );
    107 }
    108 
    109 It::~It()
    110 {
    111   printf( "It dtor done\n" );
    112 }
    113 
    114 int main()
    115 {
    116   return 0;
    117 }
    118