-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPerfectForwarding.cpp
More file actions
83 lines (60 loc) · 1.89 KB
/
PerfectForwarding.cpp
File metadata and controls
83 lines (60 loc) · 1.89 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
// =====================================================================================
// PerfectForwarding.cpp // Perfect Forwarding
// =====================================================================================
module modern_cpp:perfect_forwarding;
namespace PerfectForwarding {
static void overloaded(const int& arg) {
std::println("By lvalue: {}", arg);
}
static void overloaded(int&& arg) {
std::println("By rvalue: {}", arg);
}
/*
* Note: "T&&" with "T" being template parameter is special: Universal Reference
*/
template <typename T>
void forwarding(T&& arg) {
overloaded(arg);
}
template <typename T>
void forwardingPerfect(T&& arg) {
overloaded(std::forward<T>(arg));
}
static void test_forwarding() {
int n{ 123 };
forwarding(n);
forwarding(456);
}
static void test_forwardingPerfect() {
int n{ 123 };
forwardingPerfect(n);
forwardingPerfect(456);
}
// =================================================================================
template <typename T, typename U>
void foo(T&& arg1, U&& arg2)
{
// Beobachte den Inhalt der beiden Parameter 'arg1' und 'arg2'
// T obj1 = std::forward<T>(arg1);
// vs
T obj1 = arg1;
std::println("[{}]", arg1);
U obj2 = std::forward<U>(arg2);
std::println("[{}]", arg2);
}
static void test_example()
{
std::string s{ "DEF" };
foo(std::string{ "ABC" }, s);
}
}
void main_perfect_forwarding()
{
using namespace PerfectForwarding;
test_forwarding();
test_forwardingPerfect();
test_example();
}
// =====================================================================================
// End-of-File
// =====================================================================================