-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_rvalue.cpp
More file actions
193 lines (155 loc) · 4.97 KB
/
local_rvalue.cpp
File metadata and controls
193 lines (155 loc) · 4.97 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
191
192
193
#include <memory>
#include <print>
#include <string>
#include <cxxabi.h>
std::string demangle(const char *name) {
int status = INT_MIN;
std::unique_ptr<char[], void(*)(void *)> res(abi::__cxa_demangle(name, nullptr, nullptr, &status), std::free);
std::string out = status == 0? res.get() : name;
if (out == "Wrapper<char const*>") {
out = "Str";
} else if (out == "Wrapper<Wrapper<char const*> >" || out == "Wrapper<Str>") {
out = "StrStr";
} else if (out == "char const*") {
out = "const char *";
}
return out;
}
template <typename T>
struct Wrapper: T {
using Sub = T;
std::string name = demangle(typeid(Wrapper<T>).name());
std::string subname = demangle(typeid(T).name());
Wrapper() {
std::println("\t\x1b[2m[ {}() ]\x1b[22m {}", name, (void *) this);
}
~Wrapper() {
std::println("\t\x1b[2m[ ~{}() ]\x1b[22m {}", name, (void *) this);
}
Wrapper(const T &other):
T(other) {
std::println("\t\x1b[2m[ {}(const {} &) ]\x1b[22m {}", name, subname, (void *) this);
}
Wrapper(T &&other):
T(std::move(other)) {
std::println("\t\x1b[2m[ {}({} &&) ]\x1b[22m {}", name, subname, (void *) this);
}
Wrapper(const Wrapper<T> &other):
T(other) {
std::println("\t\x1b[2m[ {}(const {} &) ]\x1b[22m {}", name, name, (void *) this);
}
Wrapper(Wrapper<T> &&other):
T(std::move(other)) {
std::println("\t\x1b[2m[ {}({} &&) ]\x1b[22m {}", name, name, (void *) this);
}
Wrapper<T> & operator=(const T &other) {
std::println("\t\x1b[2m[ {} & operator=(const {} &) ]\x1b[22m {}", name, subname, (void *) this);
T::operator=(other);
return *this;
}
Wrapper<T> & operator=(T &&other) {
std::println("\t\x1b[2m[ {} & operator=({} &&) ]\x1b[22m {}", name, subname, (void *) this);
T::operator=(std::move(other));
return *this;
}
Wrapper<T> & operator=(const Wrapper<T> &other) {
std::println("\t\x1b[2m[ {} & operator=(const {} &) ]\x1b[22m {}", name, name, (void *) this);
T::operator=(other);
return *this;
}
Wrapper<T> & operator=(Wrapper<T> &&other) {
std::println("\t\x1b[2m[ {} & operator=({} &&) ]\x1b[22m {}", name, name, (void *) this);
T::operator=(std::move(other));
return *this;
}
};
template <>
struct Wrapper<const char *> {
using Sub = const char *;
std::string value;
std::string name = demangle(typeid(*this).name());
std::string subname = demangle(typeid(Sub).name());
Wrapper() {
std::println("\t\x1b[2m[ {}() ]\x1b[22m {}", name, (void *) this);
}
~Wrapper() {
std::println("\t\x1b[2m[ ~{}() ]\x1b[22m {}", name, (void *) this);
}
Wrapper(const char * const &other):
value(other) {
std::println("\t\x1b[2m[ {}(const {} &) ]\x1b[22m {}", name, subname, (void *) this);
}
Wrapper(const char * &&other):
value(std::move(other)) {
std::println("\t\x1b[2m[ {}({} &&) ]\x1b[22m {}", name, subname, (void *) this);
}
Wrapper(const Wrapper<const char *> &other):
value(other.value) {
std::println("\t\x1b[2m[ {}(const {} &) ]\x1b[22m {}", name, name, (void *) this);
}
Wrapper(Wrapper<const char *> &&other):
value(std::move(other.value)) {
std::println("\t\x1b[2m[ {}({} &&) ]\x1b[22m {}", name, name, (void *) this);
}
Wrapper<const char *> & operator=(const char * const &other) {
std::println("\t\x1b[2m[ {} & operator=(const {} &) ]\x1b[22m {}", name, subname, (void *) this);
value = other;
return *this;
}
Wrapper<const char *> & operator=(const char * &&other) {
std::println("\t\x1b[2m[ {} & operator=({} &&) ]\x1b[22m {}", name, subname, (void *) this);
value = std::move(other);
return *this;
}
Wrapper<const char *> & operator=(const Wrapper<const char *> &other) {
std::println("\t\x1b[2m[ {} & operator=(const {} &) ]\x1b[22m {}", name, name, (void *) this);
value = other.value;
return *this;
}
Wrapper<const char *> & operator=(Wrapper<const char *> &&other) {
std::println("\t\x1b[2m[ {} & operator=({} &&) ]\x1b[22m {}", name, name, (void *) this);
value = std::move(other.value);
return *this;
}
};
using Str = Wrapper<const char *>;
using StrStr = Wrapper<Str>;
template <>
struct std::formatter<Str> {
constexpr auto parse(auto &ctx) {
return ctx.begin();
}
auto format(const Str &str, auto &ctx) const {
return std::format_to(ctx.out(), "{}", str.value);
}
};
template <>
struct std::formatter<StrStr> {
constexpr auto parse(auto &ctx) {
return ctx.begin();
}
auto format(const StrStr &str, auto &ctx) const {
return std::format_to(ctx.out(), "<{}>", str.value);
}
};
struct Test {
Test() {
std::println("\x1b[1m<<<<<<<<<<<<<<<<\x1b[22m\n");
}
~Test() {
std::println("\n\x1b[1m>>>>>>>>>>>>>>>>\x1b[22m");
}
};
template <typename... Args>
void print(Args &&...args) {
(std::println(" {}", args), ...);
}
int main() {
{
Test test;
std::println("StrStr &&a = Str(\"foo\");"); StrStr &&a = Str("foo");
std::println("print(a);"); print(a);
}
// learnings:
// rvalue reference variables are kind of like lvalue references except they can bind only to xvalues. they don't cause any constructions or assignments on their own.
}