-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMove.cpp
More file actions
191 lines (150 loc) · 6.89 KB
/
Move.cpp
File metadata and controls
191 lines (150 loc) · 6.89 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
// sample for move assignment and constructor, based on code found on MSDN as well as cppreference.com.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <tuple>
#define LENGTH 10
// comment out following if want to see the difference in output when no move constructors / move assignment operators are defined
#define WITH_MOVE
class Dummy
{
public:
Dummy()
{
std::cout << "In Dummy(). this = " << (void*)this << std::endl;
}
Dummy(const Dummy& other)
{
std::cout << "In Dummy(const Dummy&). this = " << (void*)this << std::endl;
}
#ifdef WITH_MOVE
Dummy(const Dummy&& other)
{
std::cout << "In Dummy(const Dummy&&). this = " << (void*)this << std::endl;
}
#endif
Dummy& operator=(const Dummy& other)
{
std::cout << "In Dummy::operator=(const Dummy& other). this = " << (void*)this << std::endl;
return *this;
}
#ifdef WITH_MOVE
Dummy& operator=(const Dummy&& other)
{
std::cout << "In Dummy::operator=(const Dummy&& other). this = " << (void*)this << std::endl;
return *this;
}
#endif
~Dummy()
{
std::cout << "In ~Dummy(). this = " << (void*)this << std::endl;
}
};
class MemoryMoveBlock
{
public:
explicit MemoryMoveBlock()
: _data(new int[LENGTH])
, _dummy("Test")
{
std::cout << "Created resource " << (void*)_data << std::endl;
std::cout << "In MemoryMoveBlock(). this = " << (void*)this << " data = " << (void*)_data << "." << std::endl;
}
~MemoryMoveBlock()
{
std::cout << "In ~MemoryMoveBlock(). this = " << (void*)this << " data = " << (void*)_data << "." << std::endl;
if (_data != nullptr)
{
std::cout << "Deleting resource " << (void*)_data << std::endl;
delete[] _data;
}
}
MemoryMoveBlock(const MemoryMoveBlock& other)
: _data(new int[LENGTH])
, _dummy(other._dummy)
{
std::cout << "Created resource " << (void*)_data << std::endl;
std::cout << "In MemoryMoveBlock(const MemoryMoveBlock&). this = " << (void*)this << " data = " << (void*)_data << ". Copying resource." << std::endl;
std::copy(other._data, other._data + LENGTH, _data);
}
#ifdef WITH_MOVE
MemoryMoveBlock(MemoryMoveBlock&& other)
: _data(std::exchange(other._data, nullptr)) // explicit move of a member of non class type (note that std::exchange is c++14)
, _dummy(std::move(other._dummy)) // explicit move of a member of class type
{
std::cout << "In MemoryBlock(MemoryBlock&&). this = " << (void*)this << " data = " << (void*)_data << ". Moved resource." << std::endl;
std::cout << "After MemoryBlock(MemoryBlock&&). other = " << (void*)&other << " data = " << (void*)other._data << std::endl;
}
MemoryMoveBlock& operator=(MemoryMoveBlock&& other)
{
std::cout << "In operator=(MemoryMoveBlock&&). this = " << (void*)this << " data = " << (void*)_data << ". Moving resource." << std::endl;
std::cout << "In operator=(MemoryMoveBlock&&). other = " << (void*)&other << " data = " << (void*)other._data << std::endl;
if (this != &other) {
std::cout << "Deleting resource " << (void*)_data << std::endl;
delete[] _data;
_data = std::exchange(other._data, nullptr);
_dummy = std::move(other._dummy);
}
std::cout << "After operator=(MemoryMoveBlock&&). this = " << (void*)this << " data = " << (void*)_data << std::endl;
std::cout << "After operator=(MemoryMoveBlock&&). other = " << (void*)&other << " data = " << (void*)other._data << std::endl;
return *this;
}
#endif
MemoryMoveBlock& operator=(const MemoryMoveBlock& other)
{
std::cout << "In operator=(const MemoryMoveBlock&). this = " << (void*)this << " data = " << (void*)_data << ". Copying resource." << std::endl;
if (this != &other)
{
std::cout << "Deleting resource " << (void*)_data << std::endl;
delete[] _data;
_data = new int[LENGTH];
_dummy = other._dummy;
std::cout << "Created resource " << (void*)_data << std::endl;
std::copy(other._data, other._data + LENGTH, _data);
}
return *this;
}
const std::string& getDummy() { return _dummy; }
private:
int* _data; // The non class resource.
std::string _dummy; // The class resource
};
int main()
{
// starting with simpler class so it is easier to read what happens
{
std::vector<Dummy> v;
v.reserve(4); // avoid vector relocations which would produce additional move/copy constructor invocations
std::cout << std::endl << "Pushing first item to vector" << std::endl;
v.push_back(Dummy());
std::cout << std::endl << "Pushing second item to vector" << std::endl;
v.push_back(Dummy());
std::cout << std::endl << "Inserting third item to vector at second position" << std::endl;
v.insert(++v.begin(), Dummy());
std::cout << std::endl << "Ending" << std::endl;
}
{
std::vector<MemoryMoveBlock> v;
v.reserve(4); // avoid vector relocations which would produce additional move/copy constructor invocations
std::cout << std::endl << "Pushing first item to vector" << std::endl;
v.push_back(MemoryMoveBlock());
std::cout << std::endl << "Pushing second item to vector" << std::endl;
v.push_back(MemoryMoveBlock());
std::cout << std::endl << "Inserting third item to vector at second position" << std::endl;
v.insert(++v.begin(), MemoryMoveBlock());
std::cout << std::endl << "Ending" << std::endl;
}
{
auto blocksInTuple = std::make_tuple(MemoryMoveBlock(), MemoryMoveBlock());
auto newBlockCopy = std::get<0>(blocksInTuple); // copy constructor here
auto newBlockMoved = std::get<0>(std::move(blocksInTuple)); // move constructor here, but only for first member of tuple, second is not touched
std::cout << std::endl << std::get<0>(blocksInTuple).getDummy() << std::endl; // prints empty string (has been moved to newBlockMoved)
std::cout << std::endl << newBlockMoved.getDummy() << std::endl; // prints Test (has been moved from first member of tuple)
std::cout << std::endl << std::get<1>(blocksInTuple).getDummy() << std::endl; // prints Test (second member of tuple not touched)
auto newBlockMoved2 = std::move(std::get<1>(blocksInTuple)); // also move constructor here for member of tuple, a bit more obvious move is applied to second only
std::cout << std::endl << std::get<1>(blocksInTuple).getDummy() << std::endl; // prints empty string (has been moved to newBlockMoved)
std::cout << std::endl << newBlockMoved2.getDummy() << std::endl; // prints Test (has been moved from first member of tuple)
}
return 0;
}