Repository navigation
Expand file tree
/
Copy pathIsTransparent.cpp
More file actions
277 lines (215 loc) · 7.91 KB
/
Copy pathIsTransparent.cpp
File metadata and controls
277 lines (215 loc) · 7.91 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// =====================================================================================
// IsTransparent.cpp // is_transparent
// =====================================================================================
module modern_cpp:is_transparent;
namespace IsTransparent {
// =================================================================================
// demonstration of a problem with user defined classes and keys
class Employee
{
private:
std::size_t m_id;
std::string m_name;
public:
// c'tor
explicit Employee(std::size_t id, const std::string& name)
: m_id{ id }, m_name{ name }
{}
// getter
std::size_t getId() const { return m_id; }
std::string getName() const { return m_name; }
// operators - add / remove comment
bool operator<(const Employee& other) const
{
return m_id < other.m_id;
}
};
static void test_01()
{
std::set<Employee> employees // does not compile - enable operator< in class Employee
{
Employee{ 1, "John" },
Employee{ 2, "Bill" }
};
}
// =================================================================================
// demonstration of a problem with user defined classes and keys
class AnotherEmployee
{
private:
std::size_t m_id;
std::string m_name;
public:
// c'tor
explicit AnotherEmployee(std::size_t id, const std::string& name)
: m_id{ id }, m_name{ name }
{}
// getter
std::size_t getId() const { return m_id; }
std::string getName() const { return m_name; }
};
struct CompareId
{
using is_transparent = void; // add / remove comment
bool operator() (const AnotherEmployee& employee1, const AnotherEmployee& employee2) const
{
return employee1.getId() < employee2.getId();
}
bool operator() (std::size_t id, const AnotherEmployee& employee) const // add / remove comment
{
return id < employee.getId();
}
bool operator() (const AnotherEmployee& employee, std::size_t id) const // add / remove comment
{
return employee.getId() < id;
}
};
static void test_02()
{
std::set<AnotherEmployee, CompareId> employees
{
AnotherEmployee{ 1, "John" },
AnotherEmployee{ 2, "Bill" }
};
auto pos = employees.find(1); // still does not compile - remove *all* comments in class CompareId
std::println("Name: {}", pos->getName());
}
// =================================================================================
// demonstration of a problem with redundant type conversion operations
static void test_03()
{
std::set<std::string> strings;
std::string one{ "one" };
strings.insert(one);
strings.insert("two"); // is there a (performance) problem ???
auto pos = strings.find("one");
}
static void test_04()
{
// using is_transparent implicitly
std::set<std::string, std::less<>> strings; // std::less<> is transparent, look at source code of std::less<>
strings.insert("one");
strings.insert("two");
auto pos = strings.find("one");
}
struct MyCompare
{
// we can also demonstrate this explicitly using a custom comparator
using is_transparent = void; // add / remove comment
bool operator() (const std::string& a, const std::string& b) const
{
return a < b;
}
bool operator() (const std::string& a, const char* b) const // add / remove comment
{
return a < b;
}
bool operator() (const char* a, const std::string& b) const // add / remove comment
{
return a < b;
}
};
static void test_05()
{
std::set<std::string, MyCompare> strings;
strings.insert("one");
strings.insert("two");
auto pos = strings.find("one");
}
// =================================================================================
// Prerequisites for is_transparent
static void test_06()
{
std::set<std::string, std::less<>> strings; // since C++14
strings.insert("two"); // `insert` does not have a heterogeneous variant; a `std::string` is still created here.
auto pos = strings.find("one"); // template<class K> find(const K&), no temporary std::string
}
// =================================================================================
// Demonstration of whether conversion takes place
// Variant: Custom key type.
// Custom key type with output in the constructor.
// This allows you to see the conversion directly.
class Key
{
private:
std::string m_s;
public:
Key(const std::string& s) : m_s{ s }
{
std::println("-> No Conversion: std::string& -> Key");
}
Key(const char* cp) : m_s{ cp }
{
std::println("-> Conversion: const char* -> Key");
}
friend bool operator<(const Key& a, const Key& b) { return a.m_s < b.m_s; }
friend bool operator<(const Key& a, const char* b) { return a.m_s < b; }
friend bool operator<(const char* a, const Key& b) { return a < b.m_s; }
};
static void test_07()
{
std::set<Key> normal;
normal.insert("one"); // conversion (necessary)
std::puts("find in normal std::set:");
auto pos1 = normal.find("one"); // conversion
std::set<Key, std::less<>> transparent;
transparent.insert("one"); // conversion (necessary)
std::puts("find in transparent std::set:");
auto pos2 = transparent.find("one"); // no output, no conversion
}
// =================================================================================
// Implementing a transparent comparator involves defining a function object
// with an is_transparent type and overloads for operator() that can compare different types.
// Here's a simplified example using std::set:
struct CaseInsensitiveCompare
{
using is_transparent = void;
bool operator() (const std::string& a, const std::string& b) const {
return std::lexicographical_compare(
a.begin(),
a.end(),
b.begin(),
b.end(),
[](char ac, char bc) {
return std::tolower(ac) < std::tolower(bc);
}
);
}
bool operator() (char a, const std::string& b) const {
return std::lexicographical_compare(&a, &a + 1, b.begin(), b.end(),
[](char ac, char bc) {
return std::tolower(ac) < std::tolower(bc);
}
);
}
bool operator() (const std::string& a, char b) const {
for (char ch : a) {
if (std::tolower(ch) == std::tolower(b)) {
return true;
}
}
return false;
}
};
static void test_08()
{
std::set<std::string, CaseInsensitiveCompare> s = { "Alpha", "beta", "Gamma" };
std::println("{}", s.find("alpha") != s.end()); // true
std::println("{}", s.find('G') != s.end()); // true
}
}
void main_is_transparent()
{
using namespace IsTransparent;
test_01();
test_02();
test_03();
test_04();
test_05();
test_06();
test_07();
test_08();
}
// =====================================================================================
// End-of-File
// =====================================================================================