-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReferenceWrapper.cpp
More file actions
143 lines (97 loc) · 3.42 KB
/
ReferenceWrapper.cpp
File metadata and controls
143 lines (97 loc) · 3.42 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
// =====================================================================================
// ReferenceWrapper.cpp // Reference Wrapper // std::reference_wrapper // std::ref
// =====================================================================================
module modern_cpp:reference_wrapper;
namespace MyReferenceWrapper {
static void task (int& data) { // expecting a reference to int
data = 123;
}
static void test_01() {
int data{ 1 };
std::cout << data << std::endl;
// std::thread t{ task, data }; // doesn't compile
std::thread t{ task, std::ref(data) }; // works
t.join();
std::cout << data << std::endl;
}
static void test_02() {
/* A reference does not satisfy the STL container element requirements
* (in this case: Erasable)
*/
// std::vector<std::string&> names; // doesn't compile
std::vector<std::reference_wrapper<std::string>> names;
std::string name1{ "Hans" };
std::string name2{ "Sepp" };
std::string name3{ "Georg" };
names.push_back(std::ref(name1));
names.push_back(std::ref(name2));
names.push_back(std::ref(name3));
for (const std::string& sr : names) {
std::cout << sr << std::endl;
}
for (std::string& sr : names) {
sr.append(".");
}
for (const std::string& sr : names) {
std::cout << sr << std::endl;
}
}
static std::vector<std::reference_wrapper<std::string>> generateVector() {
std::vector<std::reference_wrapper<std::string>> names;
std::string name1{ "Hans" };
std::string name2{ "Sepp" };
std::string name3{ "Georg" };
names.push_back(std::ref(name1));
names.push_back(std::ref(name2));
names.push_back(std::ref(name3));
return names;
}
static void test_03() {
std::vector<std::reference_wrapper<std::string>> areThereNames {
generateVector()
};
for (const std::string& sr : areThereNames) {
std::cout << sr << std::endl;
}
}
// =================================================================================
/*
* Possible implementation
*/
template<typename T>
class my_reference_wrapper {
private:
T* m_ptr;
public:
my_reference_wrapper(T& t) noexcept : m_ptr{ &t } {}
operator T& () const noexcept { return *m_ptr; }
T& get() const noexcept { return *m_ptr; }
};
template<typename T>
my_reference_wrapper<T> my_ref(T& t) noexcept {
return my_reference_wrapper<T>(t);
}
static void test_04() {
int result{ 1 };
auto task = [] (int& r) {
r = 123;
};
std::cout << result << std::endl;
// std::thread t{ task, result }; // doesn't compile
// pass result "by reference" to the thread
std::thread t{task, my_ref(result)};
t.join();
std::cout << result << std::endl;
}
}
void main_reference_wrapper()
{
using namespace MyReferenceWrapper;
test_01();
test_02();
// test_03(); // fails at runtime
test_04();
}
// =====================================================================================
// End-of-File
// =====================================================================================