forked from Light-City/CPlusPlusThings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.cpp
More file actions
93 lines (81 loc) · 2.6 KB
/
Copy pathall.cpp
File metadata and controls
93 lines (81 loc) · 2.6 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
//
// Created by light on 19-12-23.
//
#include <iostream>
#include <string>
#include <vector>
struct Test {
Test() {
// std::cout << "construct a Test object" << std::endl;
}
Test(const Test &) {
std::cout << "copy construct a Test object" << std::endl;
}
Test &operator=(const Test &t) {
std::cout << "copy assignment a Test object" << std::endl;
return *this;
}
Test(Test &&t) { std::cout << "move construct a Test object" << std::endl; }
Test &operator=(Test &&t) {
std::cout << "move assignment a Test object" << std::endl;
return *this;
}
~Test() {
// std::cout << "destruct a Test object" << std::endl;
}
};
Test getTest() {
// anonymous object
return Test();
}
Test getTestWithName() {
// named return value
Test temp;
return temp;
}
/**
* 1.copy
* construct本身在RVO和NRVO两种情况下被优化了,如果再加上move反而画蛇添足。
* 2.加入了move assignment后,默认是调用move assignment而不是copy
* assignment,可以将move assignment注释后测试。
* 3.对于RVO和NRVO来说,construction的情况编译器优化得比较好了,加入move语义主要是对于assignment有比较大影响
*/
int main() {
std::cout << "==== common case ====" << std::endl;
Test o1;
std::cout << "---- Test copy construct: " << std::endl;
Test o2(o1); // two ways for copy construct
Test o3 = o1;
std::cout << "---- Test move construct: " << std::endl;
Test o4(std::move(o3));
std::cout << "---- Test assignment: " << std::endl;
o2 = o1;
std::cout << "---- Test move assignment: " << std::endl;
Test o5;
o5 = std::move(o4);
std::cout << "\n==== test for rvo ===" << std::endl;
std::cout << "---- Test rvo for copy construct: " << std::endl;
Test obj11(getTest());
Test obj1 = getTest();
std::cout << "---- Test rvo for move construct: " << std::endl;
Test obj12(std::move(getTest()));
std::cout << "---- Test rvo for assignment: " << std::endl;
Test obj2;
obj2 = getTest();
std::cout << "---- Test rvo move assignment: " << std::endl;
Test obj5;
obj5 = std::move(getTest());
std::cout << "\n==== test for nrvo ===" << std::endl;
std::cout << "---- Test nrvo for copy construct: " << std::endl;
Test obj33(getTestWithName());
Test obj3 = getTestWithName();
std::cout << "---- Test nrvo for move construct: " << std::endl;
Test obj34(std::move(getTestWithName()));
std::cout << "---- Test nrvo for assignment: " << std::endl;
Test obj4;
obj4 = getTestWithName();
std::cout << "---- Test nrvo move assignment: " << std::endl;
Test obj6;
obj6 = std::move(getTestWithName());
return 0;
}