-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaug_code.cpp
More file actions
206 lines (171 loc) · 4.4 KB
/
aug_code.cpp
File metadata and controls
206 lines (171 loc) · 4.4 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <vector>
#include <sstream>
#include <map>
#include <memory>
#include <algorithm>
template <typename T>
class mySP
{
public:
mySP()
{
refcount = 1;
std::cout << __func__ << ": inside default constructor: refcount is: " << refcount << std::endl;
ptr = new T;
} //; //= default;
mySP(T args)
{
refcount = 1;
std::cout << __func__ << ": inside default constructor: refcount is: " << refcount << std::endl;
ptr = new T(args);
}
mySP<T>& operator=(mySP<T>& a)
{
refcount = a.refcount + 1;
std::cout << __func__ << ": inside assign constructor: refcount is: " << refcount << std::endl;
delete ptr;
ptr = a.ptr;
return *this;
}
mySP (const mySP<T>& a) {
refcount = a.refcount + 1;
std::cout << __func__ << ": inside copy constructor: refcount is: " << refcount << std::endl;
ptr = a.ptr;
}
~mySP()
{
refcount--;
if ((refcount == 0) && (ptr != nullptr))
{
std::cout << __func__ << ": inside default destructor: delete ptr, refcount is: " << refcount << std::endl;
delete ptr;
ptr = nullptr;
}
else
{
std::cout << __func__ << ": inside default destructor: refcount is: " << refcount << std::endl;
}
} //= default;
T operator*()
{
return *ptr;
}
T *operator->()
{
return ptr;
}
private:
uint32_t refcount;
T *ptr{nullptr};
};
template<typename T1, typename T2>
auto add(T1 a, T2 b) -> decltype (a+b)
{
return a + b;
}
using namespace std;
void dosomethingTocrash();
void testTraits();
void testSP();
void testmemoryleak();
void testdecltype();
void testlamdas() ;
void testvisitor() ;
int main(int argc, char *argv[])
{
dosomethingTocrash();
// std::cout << "*up is again " << *up << std::endl;
testTraits();
testSP();
testmemoryleak();
testdecltype();
testlamdas() ;
testvisitor();
}
void testdecltype()
{
std::cout << __func__ << ": 1 + 2 : " << add (1,2) << std::endl;
std::cout << __func__ << ": 1.2 + 2 : " << add (1.2,2) << std::endl;
std::cout << __func__ << ": 1 + 2.2 : " << add (1,2.2) << std::endl;
}
//
//
//
void testSP()
{
mySP<int> sp = mySP<int>(3.0);
std::cout << __func__ << ": *sp is " << *sp << std::endl;
typedef struct justint
{
int a{4};
} justint_t;
using mytypeT = justint_t;
mytypeT t;
t.a = 5;
auto sps = mySP<mytypeT>(t);
std::cout << __func__ << ": *sps->a: is " << sps->a << std::endl;
mySP<int> sp2 = mySP<int>(4.0);
std::cout << __func__ << ": default *sp2 is " << *sp2 << std::endl;
std::cout << "about to call copy assignment operator for sp2" << std::endl;
sp2 = sp;
std::cout << __func__ << ": *sp2 is " << *sp2 << std::endl;
std::cout << "about to call copy constructor for sp3" << std::endl;
mySP<int> sp3 = sp;
std::cout << __func__ << ": *sp3 is " << *sp3 << std::endl;
}
//
//
//
void dosomethingTocrash()
{
cout << "master" << endl;
std::vector<int> vec = {1, 2, 3};
for (auto const e : vec)
{
std:;
cout << e << std::endl;
}
std::cout << __func__ << ": vec[0] is " << vec[0] << std::endl;
//std::cout << "vec[5000] is " << vec[5000] << std::endl;
auto up = make_unique<int>();
//up = nullptr; Anton nov 2 2022
*up = 5;
std::cout << __func__ << ": *up is " << *up << std::endl;
}
//
//
//
void testmemoryleak()
{
// int* p = new int(1);
//std::cout << __func__ << ": p is new int allocated but not deleted " << std::endl;
}
//
//
//
void testTraits()
{
std::cout << __func__ << ": is float<int>: " << std::is_floating_point<int>::value << std::endl;
std::cout << __func__ << ": is float<float>: " << std::is_floating_point<float>::value << std::endl;
}
void testlamdas() {
auto ptr = std::make_shared<int>(1);
int pqr = 1;
auto f = [=](int &y)
{ std::cout << __func__ << ": hi2: " << *ptr << std::endl; };
f(pqr);
}
//
//
//
std::vector<int> myvec = {1,2,3};
template <typename T>
void visitor(std::vector<T> &vec)
{
for_each(vec.begin(), vec.end(), [](const T&a ){ std::cout << __func__ << ": << element a is: " << a << std::endl; });
}
void testvisitor() {
std::cout << __func__ << ": testing visitor: " << std::endl;
visitor<int>(myvec);
}