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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
//============================================================================
// Author : Sven Gothel
// Copyright : 2022 Gothel Software e.K.
// License : MIT
// Description : C++ Lesson 1.1 Memory (lifecycle)
//============================================================================
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <string>
#include <memory>
#include <atomic>
#include <iostream>
/**
* Lesson 1.1
*
* Memory occupying object lifecycle:
* - Construction (default, copy, move)
* - Assigning (copy, move)
* - Referencing
* - Destruction.
*/
class sint_t; // forward
std::ostream& operator<<(std::ostream& out, const sint_t& v);
class sint_t {
private:
static std::atomic_int last_id;
int id;
int64_t store;
public:
/** Default constructor (ctor) */
sint_t() noexcept
: id(++last_id), store(0) {
std::cout << "ctor.norm: " << *this << std::endl;
}
/** Copy ctor */
sint_t(const sint_t& o) noexcept
: id(++last_id), store(o.store) {
std::cout << "ctor.copy: " << o << " -> " << *this << std::endl;
}
/** Move ctor */
sint_t(sint_t&& o) noexcept
: id( o.id ), store( o.store ) {
std::cout << "ctor.move: " << o << " -> " << *this << ", old ";
o.id = 0;
std::cout << o << std::endl;
}
/** Destructor (dtor) */
~sint_t() noexcept {
std::cout << "ctor.dtor: " << *this << std::endl;
id = 0;
}
/** Explicit conversion ctor: int64_t -> my_int_t */
explicit sint_t(const int64_t& o) noexcept
: id(++last_id), store( o ) {
std::cout << "ctor.conv: " << std::to_string( o ) << " -> " << *this << std::endl;
}
//
// Assignment operations, including arithmetic assignments
//
/** Assignment */
sint_t& operator=(const sint_t& r) noexcept {
if( this != &r ) {
store = r.store;
std::cout << "assign.copy: " << r << " -> " << *this << std::endl;
} else {
std::cout << "assign.self: " << *this << std::endl;
}
return *this;
}
/** Move-Assignment */
sint_t& operator=(sint_t&& r) noexcept {
if( this != &r ) {
std::cout << "assign.move: " << r << " -> ";
id = r.id;
store = r.store;
r.id = 0;
std::cout << *this << ", old " << r << std::endl;
} else {
std::cout << "assign.self: " << *this << std::endl;
}
return *this;
}
//
// Misc operations
//
sint_t& set(int64_t v) noexcept { store = v; return *this; }
int64_t get() const noexcept { return store; }
std::string to_string() const noexcept { return "[id "+std::to_string(id)+", val "+std::to_string(store)+"]"; }
};
std::atomic_int sint_t::last_id(0);
inline std::ostream& operator<<(std::ostream& out, const sint_t& v) {
return out << v.to_string();
}
sint_t make_obj(int64_t v) { return sint_t(v); }
const sint_t& filter10(const sint_t& o) { return o; }
sint_t filter11(const sint_t& o) { return o; }
sint_t& filter20(sint_t& o) { o.set(o.get()+1); return o; }
sint_t filter21(sint_t& o) { o.set(o.get()+1); return o; }
int main(int, const char* []) {
std::cout << "Block 1" << std::endl;
{
sint_t a;
sint_t c(a);
sint_t d = a;
(void)a; (void)c; (void)d;
// ctor.norm: [id 1, val 0]
// ctor.copy: [id 1, val 0] -> [id 2, val 0]
// ctor.copy: [id 1, val 0] -> [id 3, val 0]
// ctor.dtor: [id 3, val 0]
// ctor.dtor: [id 2, val 0]
// ctor.dtor: [id 1, val 0]
}
std::cout << "Block 2" << std::endl;
{
const sint_t& a = make_obj(1);
std::cout << "b2.a: " << a << std::endl;
const sint_t& b = filter10(a);
std::cout << "b2.b: " << b << std::endl;
(void)a; (void)b;
// ctor.conv: 1 -> [id 4, val 1]
// b2.a: [id 4, val 1]
// b2.b: [id 4, val 1]
// ctor.dtor: [id 4, val 1]
}
std::cout << "Block 3" << std::endl;
{
sint_t a = make_obj(1);
std::cout << "b3.a: " << a << std::endl;
sint_t b = make_obj(2) ;
std::cout << "b3.b: " << b << std::endl;
// ctor.conv: 1 -> [id 5, val 1]
// b3.a: [id 5, val 1]
// ctor.conv: 2 -> [id 6, val 2]
// ctor.move: [id 6, val 2] -> [id 6, val 2], old [id 0, val 2]
// ctor.dtor: [id 0, val 2]
}
std::cout << "Block 4" << std::endl;
{
sint_t a, b(2);
std::cout << "b4.0.a: " << a << std::endl;
std::cout << "b4.0.b: " << b << std::endl;
a = std::move( b ); // b should not be dereferenced after move
std::cout << "b4.1.a: " << a << std::endl;
a = make_obj(3) ;
std::cout << "b4.2.a: " << a << std::endl;
// ctor.norm: [id 7, val 0]
// ctor.conv: 2 -> [id 8, val 2]
// b4.0.a: [id 7, val 0]
// b4.0.b: [id 8, val 2]
// assign.move: [id 8, val 2] -> [id 8, val 2], old [id 0, val 2]
// b4.1.a: [id 8, val 2]
// ctor.conv: 3 -> [id 9, val 3]
// assign.move: [id 9, val 3] -> [id 9, val 3], old [id 0, val 3]
// ctor.dtor: [id 0, val 3]
// b4.2.a: [id 9, val 3]
// ctor.dtor: [id 0, val 2]
// ctor.dtor: [id 9, val 3]
}
return 0;
}
|