forked from albertz/playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_init_time.cpp
More file actions
82 lines (69 loc) · 1.13 KB
/
static_init_time.cpp
File metadata and controls
82 lines (69 loc) · 1.13 KB
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
#include <iostream>
#include <string>
using namespace std;
struct A {
A(string s) { cout << s << endl; }
};
struct A2 {
A2() { cout << (long)this << endl; }
};
struct B {
void test1(string prefix) {
A a(prefix + ":1");
A2 a2;
}
void test2(string prefix) {
static A a(prefix + ":2");
static A2 a2;
}
static void test3(string prefix) {
A a(prefix + ":3");
A2 a2;
}
static void test4(string prefix) {
static A a(prefix + ":4");
static A2 a2;
}
void test(string prefix) {
test1(prefix);
test2(prefix);
test3(prefix);
test4(prefix);
}
};
void test(string prefix) {
B b1;
b1.test(prefix + "-1");
static B b2;
b2.test(prefix + "-2");
}
struct C {
C() { test("C"); }
};
static C c;
// ----
struct Foo {
Foo();
};
struct S {
bool inited;
S() : inited(false) {
cout << "S()" << endl;
//Foo foo; // this causes a runtime error
inited = true;
cout << "S() end" << endl;
}
static S* get() {
static S s;
return &s;
}
};
Foo::Foo() {
cout << "Foo(" << (long)this << "):" << endl;
cout << "Foo(): S.inited: " << S::get()->inited << endl;
}
static Foo foo;
// ----
int main() {
test("main");
}