-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSpaceshipOperator.cpp
More file actions
370 lines (298 loc) · 10.1 KB
/
Copy pathSpaceshipOperator.cpp
File metadata and controls
370 lines (298 loc) · 10.1 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// =====================================================================================
// SpaceshipOperator.cpp
// =====================================================================================
module;
#include <cctype>
module modern_cpp:spaceship_operator;
namespace Comparison_Problem_Classic
{
struct Version
{
int major;
int minor;
int patch;
};
// operator<
static bool operator<(const Version& a, const Version& b) {
if (a.major != b.major) return a.major < b.major;
if (a.minor != b.minor) return a.minor < b.minor;
return a.patch < b.patch;
}
// remaining 5 implementations
//static bool operator>(const Version& a, const Version& b) { // remove comment
// return b < a;
//}
//static bool operator<=(const Version& a, const Version& b) {
// return !(b < a);
//}
//static bool operator>=(const Version& a, const Version& b) {
// return !(a < b);
//}
//static bool operator==(const Version& a, const Version& b) {
// return a.major == b.major && a.minor == b.minor && a.patch == b.patch;
//}
//static bool operator!=(const Version& a, const Version& b) {
// return !(a == b);
//}
static void comparison_01()
{
std::println("spaceship_01");
struct Version firstVersion { 1, 1, 1 };
struct Version anotherVersion { 1, 1, 1 };
if (firstVersion < anotherVersion) {
std::println("firstVersion is older"); // compiles, works
}
// remove comment
//if (firstVersion > anotherVersion) {
// std::println("firstVersion is newer"); // error: no operator>
//}
//if (firstVersion == anotherVersion) {
// std::println("same version"); // error: no operator==
//}
}
}
// ======================================================================================
namespace Comparison_Categories
{
static void comparison_categories_01_strong()
{
std::println("int");
int a = 1;
int b = 2;
auto result = a <=> b; // std::strong_ordering
if (result < 0) {
std::println("a < b");
}
if (result <= 0) {
std::println("a < b");
}
if (result > 0) {
std::println("a < b");
}
if (result >= 0) {
std::println("a > b");
}
if (result == 0) {
std::println("a < b");
}
if (result != 0) {
std::println("a == b");
};
}
static void comparison_categories_02_partial()
{
std::println("double");
double a = 1.5;
double b = 2.5;
auto result = a <=> b; // std::partial_ordering
if (result < 0) {
std::println("a < b");
}
if (result <= 0) {
std::println("a < b");
}
if (result > 0) {
std::println("a < b");
}
if (result >= 0) {
std::println("a > b");
}
if (result == 0) {
std::println("a < b");
}
if (result != 0) {
std::println("a == b");
}
}
// Note: No, there is no fundamental data type or standard library (STL) data type in C++
// whose native three-way comparison operator (<=>) returns std::weak_ordering.
// How to enforce std::weak_orderingstd::weak_ordering is meant to be a deliberate, user - chosen semantic.
// It is used when two objects can be considered mathematically "equivalent" without being completely identical
// or substitutable in every function.If you want a type to use std::weak_ordering, you must manually define it.
// The classic textbook example is a case-insensitive string wrapper
class CaseInsensitiveString
{
private:
std::string m_str;
public:
CaseInsensitiveString() = default;
CaseInsensitiveString(std::string str) : m_str{ str } {}
// explicitly enforce weak_ordering
std::weak_ordering operator <=> (const CaseInsensitiveString& other) const {
// compare lengths or transform characters to lowercase
std::string a{ m_str };
std::string b{ other.m_str };
std::transform(a.begin(), a.end(), a.begin(), ::tolower);
std::transform(b.begin(), b.end(), b.begin(), ::tolower);
return a <=> b; // std::string's <=> returns strong_ordering,
// but we return std::weak_ordering to signal "equivalent, not equal"
}
bool operator== (const CaseInsensitiveString& other) const {
return (*this <=> other) == 0;
}
};
static void comparison_categories_03_weak()
{
CaseInsensitiveString s1{ "Hello" };
CaseInsensitiveString s2{ "hello" };
// evaluation returns std::weak_ordering::equivalent;
// they are equivalent, but NOT identical/substitutable (e.g., s1.str != s2.str)
auto result = s1 <=> s2;
if (result == std::weak_ordering::equivalent) {
std::println("s1 and s2 are equivalent!");
}
}
}
namespace Comparison_With_Spaceship_Operator
{
struct AnyType
{
int m_x;
int m_y;
// custom implementation of <=>
std::strong_ordering operator<=>(const AnyType& other) const noexcept {
// 1. compare first attribute
if (auto cmp = m_x <=> other.m_x; cmp != 0) {
return cmp;
}
// 2. compare the second attribute if the first was equal
return m_y <=> other.m_y;
}
// Important: The == operator MUST be defined separately:
// bool operator==(const AnyType&) const = default;
};
static void spaceship_01_struct_any_type_01()
{
AnyType a{ 1, 2 };
AnyType b{ 2, 1 };
auto result = a <=> b; // std::strong_ordering
if (result < 0) {
std::println("a < b");
}
if (result <= 0) {
std::println("a <= b");
}
if (result > 0) {
std::println("a > b");
}
if (result >= 0) {
std::println("a >= b");
}
if (result == 0) {
std::println("a == b");
}
if (result != 0) {
std::println("a != b");
}
}
static void spaceship_01_struct_any_type_02()
{
AnyType a{ 1, 2 };
AnyType b{ 2, 1 };
auto result = a <=> b; // std::strong_ordering
if (a < b) {
std::println("a < b");
}
if (a <= b) {
std::println("a < b");
}
if (a > b) {
std::println("a < b");
}
if (a >= b) {
std::println("a > b");
}
//if (a == b) { // does not compile
// std::println("a < b");
//}
//if (a != b) { // does not compile
// std::println("a == b");
//}
}
}
namespace Comparison_With_Spaceship_Operator
{
struct Version {
int major;
int minor;
int patch;
auto operator<=>(const Version&) const = default;
//std::strong_ordering operator<=>(const Version& other) const {
// if (auto cmp = major <=> other.major; cmp != 0) return cmp;
// if (auto cmp = minor <=> other.minor; cmp != 0) return cmp;
// return patch <=> other.patch;
//}
};
static void spaceship_01_struct_version()
{
struct Version firstVersion{ 1, 1, 1 };
struct Version anotherVersion{ 1, 1, 1 };
auto result = (firstVersion == anotherVersion);
std::println("result: {}", result);
result = (firstVersion != anotherVersion);
std::println("result: {}", result);
}
static void spaceship_02_struct_version()
{
struct Version firstVersion { 1, 2, 0 };
struct Version anotherVersion { 1, 3, 0 };
auto result = (firstVersion < anotherVersion);
std::println("result: {}", result);
result = (firstVersion != anotherVersion);
std::println("result: {}", result);
}
}
namespace Spaceship_Operator_Default_Implementation
{
struct Point
{
int m_x; // strong_ordering
int m_y; // strong_ordering
double m_z; // partial_ordering <== weakens the whole thing
auto operator<=>(const Point&) const = default; // deduces return type: std::partial_ordering
};
static void spaceship_01_default_implementation()
{
struct Point firstPoint { 1, 2, 5.5 };
struct Point anotherPoint { 1, 3, 6.6 };
auto result = (firstPoint < anotherPoint);
std::println("result: {}", result);
auto category = firstPoint <=> anotherPoint;
}
}
namespace Spaceship_Operator_Custom_Implementation
{
struct Employee
{
std::size_t m_id;
std::string m_name;
double m_salary;
// only want to compare by id — salary and name don't matter for ordering
std::strong_ordering operator<=>(const Employee& other) const {
return m_id <=> other.m_id;
}
// must define == separately
bool operator==(const Employee& other) const {
return m_id == other.m_id;
}
};
static void spaceship_01_custom_implementation()
{
struct Employee employee { 100, "Mueller", 5000.0 };
struct Employee anotherEmployee { 101, "Meier", 5000.0 };
if (employee < anotherEmployee) {
std::println("Mueller was hired earlier.");
}
}
}
void main_spaceship_operator()
{
Comparison_Problem_Classic::comparison_01();
Comparison_Categories::comparison_categories_01_strong();
Comparison_Categories::comparison_categories_02_partial();
Comparison_Categories::comparison_categories_03_weak();
Comparison_With_Spaceship_Operator::spaceship_01_struct_version();
Comparison_With_Spaceship_Operator::spaceship_02_struct_version();
Spaceship_Operator_Default_Implementation::spaceship_01_default_implementation();
Spaceship_Operator_Custom_Implementation::spaceship_01_custom_implementation();
}