-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_12_PerfectForwarding.cpp
More file actions
245 lines (200 loc) · 6.77 KB
/
Copy pathExercises_12_PerfectForwarding.cpp
File metadata and controls
245 lines (200 loc) · 6.77 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// =====================================================================================
// Exercises_12_PerfectForwarding.cpp
// =====================================================================================
module modern_cpp_exercises:perfect_forwarding;
import std;
namespace Exercises_PerfectForwarding {
namespace Exercise_01
{
struct Point
{
int m_x;
int m_y;
};
}
}
namespace std
{
using namespace Exercises_PerfectForwarding::Exercise_01;
// formatter for struct Point
template<>
struct std::formatter<Point> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const Point& pt, std::format_context& ctx) const {
return
std::format_to(ctx.out(), "[{},{}]", pt.m_x, pt.m_y);
}
};
// formatter for std::complex
template<typename U>
struct std::formatter<std::complex<U>> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin();
}
auto format(const std::complex<U>& c, std::format_context& ctx) const {
return
std::format_to(ctx.out(), "[{},{}]", c.real(), c.imag());
}
};
}
namespace Exercises_PerfectForwarding {
namespace Exercise_01 {
template <typename T>
void list(T&& value)
{
std::println("{}", std::forward<T>(value));
}
template <typename T, typename ... TREST>
void list(T&& first, TREST&& ... rest)
{
std::println("{}", std::forward<T>(first));
list(std::forward<TREST>(rest)...);
}
static void test_01()
{
int n{ 123 };
const double Pi{ 3.14 };
list(10, "abc", n, Pi, 2.4, std::string{ "ABC" }, 99.99f);
}
static void test_02()
{
Point p{ 11, 12 };
Point* pp = new Point{ 3, 4 };
std::complex<double> c{ 2.5, 3.5 };
list(c, 10, "abc", p, *pp, 2.4, Point{ 1, 2 });
delete pp;
}
// Lösung zur Ergänzungsaufgabe:
template <typename T>
void print(int index, T&& value)
{
std::println("{}: {}", index, std::forward<T>(value));
}
template <typename T, typename ... TREST>
void list_internal(int index, T&& first, TREST&& ... rest)
{
index++;
print(index, std::forward<T>(first));
if constexpr (sizeof...(rest) > 0) {
list_internal(index, std::forward<TREST>(rest)...);
}
}
template <typename T, typename ... TREST>
void listEx(T&& first, TREST&& ... rest)
{
int index = 1;
print(index, std::forward<T>(first));
list_internal(index, std::forward<TREST>(rest)...);
}
static void test_03() {
int n = 123;
const double pi = 3.14;
listEx(10, "abc", n, pi, 2.4, std::string("ABC"), 99.99f);
}
static void test_04() {
Point p{ 11, 12 };
Point* pp = new Point{ 3, 4 };
std::complex<double> c(2.5, 2.5);
listEx(c, 10, "abc", p, *pp, 2.4, Point{ 1, 2 });
delete pp;
}
static void test_05() {
listEx(10, 11, 12, 13, 14);
}
// Lösung zur Ergänzungsaufgabe:
// Zweite alternative Realisierung mit std::initializer_list<T>
template <typename ... T>
void listExEx(T&& ... args)
{
std::initializer_list<int>
{
(std::println("{}", std::forward<T>(args)), int{}) ...
};
}
template <typename ... T>
void listExExEx(T&& ... args)
{
std::size_t count{};
std::initializer_list<int>
{
(std::println("{}: {}", ++count, std::forward<T>(args)), int{}) ...
};
}
static void test_06() {
listExEx(100, 101, 102, 103, 104, 105);
listExExEx(100, 101, 102, 103, 104, 105);
}
static void test_07() {
Point p{ 11, 12 };
Point* pp = new Point{ 3, 4 };
std::complex<double> c(2.5, 2.5);
listExEx(c, 10, "abc", p, *pp, 2.4, Point{ 1, 2 });
listExExEx(c, 10, "abc", p, *pp, 2.4, Point{ 1, 2 });
delete pp;
}
static void testExercise()
{
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
test_07();
}
}
namespace Exercise_02 {
static void f()
{
// simulate some work (function without parameters)
std::println("calling f");
std::this_thread::sleep_for(std::chrono::seconds(2));
}
static void g(int a, double b)
{
// simulate some work (function with parameters)
std::println("calling g with parameters {} and {}", a, b);
std::this_thread::sleep_for(std::chrono::seconds(3));
}
class ExecutionTimer
{
public:
template <typename F, typename... Args>
static std::chrono::milliseconds duration(F&& f, Args&&... args)
{
// Zeitmessung in Millisekunden
std::chrono::system_clock::time_point start{ std::chrono::system_clock::now() };
auto tpStart{ std::chrono::time_point_cast<std::chrono::milliseconds>(start) };
std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
std::chrono::system_clock::time_point end{ std::chrono::system_clock::now() };
auto tpEnd{ std::chrono::time_point_cast<std::chrono::milliseconds>(end) };
std::chrono::milliseconds diff = tpEnd - tpStart;
return diff;
}
};
static void test_01() {
std::chrono::milliseconds time{ ExecutionTimer::duration(f) };
std::println("{} msecs.", time.count());
}
static void test_02() {
std::chrono::milliseconds time{ ExecutionTimer::duration(g, 10, 20.0) };
std::println("{} msecs.", time.count());
}
static void testExercise()
{
test_01();
test_02();
}
}
}
void test_exercices_perfect_forwarding()
{
using namespace Exercises_PerfectForwarding;
Exercise_01::testExercise();
Exercise_02::testExercise();
}
// =====================================================================================
// End-of-File
// =====================================================================================