-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_rxv_ub.cpp
More file actions
125 lines (116 loc) · 3.19 KB
/
Copy path02_rxv_ub.cpp
File metadata and controls
125 lines (116 loc) · 3.19 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
#include <HXprint/print.h>
// 返回值测试 1
auto __test01__ = []{
HX::print::println("Test01 {");
struct A {
A&& todo() && {
return std::move(*this);
}
static A&& mk() {
return std::move(A{}.todo());
}
~A() noexcept {
HX::print::println("~A ", this);
}
};
{
auto&& a = A::mk();
(void)a;
HX::print::println("--- mk End a ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
decltype(auto) b = A::mk();
(void)b;
HX::print::println("--- mk End b ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
auto c = A::mk(); // 返回的 A&& 通过移动构造出新的的 A; 而不是延续之前的 A&&
(void)c;
HX::print::println("--- mk End c ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
auto func = [](A a) {
(void)a;
HX::print::println("func\n");
return;
};
func(A::mk()); // 返回的 A&& 通过移动构造出新的的 A; 而不是延续之前的 A&&
HX::print::println("--- mk End d ---\n");
}
HX::print::println("--- --- --- --- ---\n");
HX::print::println("} // Test01");
return 0;
}();
#include <chrono>
#include <optional>
#include <map>
// 返回值测试 2
auto __test02__ = []{
HX::print::println("Test02 {");
struct A {
A&& todo() && {
return std::move(*this);
}
static A&& mk() {
return std::move(A{}.todo());
}
~A() noexcept {
HX::print::println("~A ", this);
(void)_p;
}
private:
using MdTree = std::multimap<std::chrono::system_clock::time_point, int>;
std::optional<MdTree::iterator> _p{};
};
struct ABuild {
A&& build() && {
return std::move(A{}.todo());
}
~ABuild() noexcept {
HX::print::println("~ABuild ", this);
}
};
auto makeBuild = []{
return ABuild{};
};
{
auto&& a = makeBuild().build();
(void)a;
HX::print::println("--- mk End a ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
decltype(auto) b = makeBuild().build();
(void)b;
HX::print::println("--- mk End b ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
auto c = makeBuild().build();
(void)c;
HX::print::println("--- mk End c ---\n");
}
HX::print::println("--- --- --- --- ---\n");
{
auto func = [](A a) {
(void)a;
HX::print::println("func\n");
return;
};
func(makeBuild().build()); // ub
// 调试的时候就可以发现:
// return std::move(A{}.todo());
// 发生了析构, 才进行返回!
// 因此访问的是悬挂引用!
HX::print::println("--- mk End d ---\n");
}
HX::print::println("--- --- --- --- ---\n");
HX::print::println("} // Test02");
return 0;
}();
int main() {
return 0;
}