-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove.cpp
130 lines (102 loc) · 2.12 KB
/
Move.cpp
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
#include <iostream>
#include <vector>
#include <type_traits>
using namespace std;
vector<int> doubleValues(const vector<int>& v)
{
vector<int> vd;
for (auto& foo : v)
vd.push_back(2 * foo);
return vd;
}
class TestClass
{
int _i;
string _str;
public:
TestClass(int i, const string& str): _i(i), _str(str) {}
TestClass(const TestClass& tc): _i(tc._i), _str(tc._str)
{
cout << "copy constructor" << endl;
}
TestClass(TestClass&& tc): _i(tc._i), _str(move(tc._str))
{
cout << "move constructor" << endl;
}
TestClass& operator=(const TestClass& tc)
{
_i = tc._i;
_str = tc._str;
cout << "copy assignment operator" << endl;
return *this;
}
TestClass& operator=(TestClass&& tc)
{
_i = tc._i;
_str = move(tc._str);
cout << "move assignment operator" << endl;
return *this;
}
void increment()
{
++_i;
}
};
TestClass modifyTestClass(const TestClass& tc)
{
TestClass tc1 = tc;
tc1.increment();
//cout << "-------a2------\n";
return tc1;
}
class SmallClass
{
int _i;
public:
SmallClass(int i) noexcept : _i(i) {}
SmallClass(const SmallClass& sc) noexcept : _i(sc._i)
{
}
SmallClass(SmallClass&& sc) noexcept : _i(sc._i)
{
}
SmallClass& operator=(const SmallClass& sc) noexcept
{
_i = sc._i;
return *this;
}
SmallClass& operator=(SmallClass&& sc) noexcept
{
_i = sc._i;
return *this;
}
};
int main()
{
// vector of int's
vector<int> v = { 1, 4, 9, -2, -12, 11, };
vector<int> vd = doubleValues(v);
vd = v;
vd = ref(v);
// noexcept
cout << v._Mylast() << "\t" << v._Myend() << "\t" << v._Myend() - v._Mylast() << endl;
v.push_back(10);
cout << v._Mylast() << "\t" << v._Myend() << "\t" << v._Myend() - v._Mylast() << endl;
v.push_back(11);
cout << v._Mylast() << "\t" << v._Myend() << "\t" << v._Myend() - v._Mylast() << endl;
// bespoke class with move syntax
TestClass tc(1, "test");
TestClass tc2 = tc;
TestClass tc3 = ref(tc);
TestClass tc4 = move(tc);
tc3 = tc;
tc3 = ref(tc);
tc4 = move(tc);
cout << "-------a1------\n";
tc2 = modifyTestClass(tc);
// type traits
int i = 56;
int* pi = &i;
//pointer_traits<pi>::element_type j = 66;
return 0;
}