forked from mcpp-community/d2mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-move-semantics-1.cpp
More file actions
100 lines (82 loc) · 2.61 KB
/
05-move-semantics-1.cpp
File metadata and controls
100 lines (82 loc) · 2.61 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
// d2mcpp: https://github.com/mcpp-community/d2mcpp
// license: Apache-2.0
// file: dslings/cpp11/05-move-semantics-0.cpp
//
// Exercise/练习: cpp11 | 05 - move semantics | 移动赋值与触发时机
//
// Tips/提示: 观察编译器输出, 在不改变buff传递的逻辑, 使得只做一次资源的分配和释放
//
// Docs/文档:
// - https://en.cppreference.com/w/cpp/language/move_constructor
//
// Auto-Checker/自动检测命令:
//
// d2x checker move-semantics
//
#include <d2x/cpp/common.hpp>
#include <iostream>
static int move_assignment_counter = 0;
struct Buffer {
int *data;
Buffer() : data { new int[2] {0, 1} } {
std::cout << "Buffer():" << data << std::endl;
}
Buffer(const Buffer &other) {
std::cout << "Buffer(const Buffer&):" << data << std::endl;
data = new int[2];
data[0] = other.data[0];
data[1] = other.data[1];
}
Buffer(Buffer&& other) : data { other.data } {
std::cout << "Buffer(Buffer&&):" << data << std::endl;
other.data = nullptr; // 让原对象的指针失效
}
Buffer & operator=(const Buffer &other) {
std::cout << "Buffer& operator=(const Buffer&):" << data << std::endl;
if (this != &other) {
delete[] data; // 释放旧资源
data = new int[2];
data[0] = other.data[0];
data[1] = other.data[1];
}
return *this;
}
Buffer & operator=(Buffer&& other) {
move_assignment_counter++;
std::cout << "Buffer& operator=(Buffer&&):" << data << std::endl;
if (this != &other) {
delete[] data; // 释放旧资源
data = other.data; // 转移资源
other.data = nullptr; // 让原对象的指针失效
}
return *this;
}
~Buffer() {
if (data) {
std::cout << "~Buffer():" << data << std::endl;
delete[] data;
}
}
const int * data_ptr() const {
std::cout << "data[0] = " << data[0] << ", data[1] = " << data[1] << std::endl;
return data;
}
};
Buffer process(Buffer buff) {
std::cout << "process(): " << buff.data << std::endl;
return buff;
}
int main() { // 无编译器优化
{
Buffer buff1;
buff1 = Buffer(); // 情况1: 临时对象赋值
d2x_assert_eq(move_assignment_counter, 1);
Buffer buff2;
buff2 = process(buff1); // 情况2: 中间对象赋值
d2x_assert_eq(move_assignment_counter, 2);
buff2 = buff1; // 情况3: 显示移动赋值
d2x_assert_eq(move_assignment_counter, 3);
}
D2X_WAIT
return 0;
}