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
17Init::Init()
18Init::use_it
19It ctor done    <-- 0
20Init::use_it done
21Init ctor done  <-- 1
22Init2 ctor done <-- 2
23It dtor done    <-- 0
24Init2 dtor done <-- 2
25Init dtor done  <-- 1
26
27
28 * but should:
29
30Init::Init()
31Init::use_it
32It ctor done    <-- 0
33Init::use_it done
34Init ctor done  <-- 1
35Init2 ctor done <-- 2
36Init2 dtor done <-- 2
37Init dtor done  <-- 1
38It dtor done    <-- 0
39
40
41 */
42#include <stdio.h>
43
44using namespace std;
45
46class Init
47{
48  public:
49    Init();
50    ~Init();
51
52    static void use_it();
53};
54
55class Init2
56{
57  public:
58    Init2();
59    ~Init2();
60
61};
62
63static Init init;
64static Init2 init2;
65
66class It
67{
68  public:
69    It();
70    ~It();
71};
72
73Init::Init()
74{
75  printf( "Init::Init()\n" );
76  use_it();
77  printf( "Init ctor done\n" );
78}
79
80Init::~Init()
81{
82  printf( "Init dtor done\n" );
83}
84
85void 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
94Init2::Init2()
95{
96  printf( "Init2 ctor done\n" );
97}
98
99Init2::~Init2()
100{
101  printf( "Init2 dtor done\n" );
102}
103
104It::It()
105{
106  printf( "It ctor done\n" );
107}
108
109It::~It()
110{
111  printf( "It dtor done\n" );
112}
113
114int main()
115{
116  return 0;
117}
118