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