-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComparison.cpp
106 lines (85 loc) · 1.98 KB
/
Comparison.cpp
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
// cpp.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
struct WrapperTwo
{
int y;
};
struct WrapperOne
{
int x;
};
bool operator==(const WrapperOne& w1, const WrapperTwo& w2)
{
return w1.x == w2.y;
}
#ifdef __cpp_lib_three_way_comparison
std::strong_ordering operator<=>(const WrapperOne& w1, const WrapperTwo& w2)
{
return w1.x <=> w2.y;
}
#endif
#ifndef __cpp_lib_three_way_comparison
// for C++17 and earlier in addition to == we need !=, <, <=, >, >= and asymmetric versions of each
bool operator==(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 == w2;
}
bool operator!=(const WrapperOne& w1, const WrapperTwo& w2)
{
return w1.x != w2.y;
}
bool operator!=(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 != w2;
}
bool operator<(const WrapperOne& w1, const WrapperTwo& w2)
{
return w1.x < w2.y;
}
bool operator<=(const WrapperOne& w1, const WrapperTwo& w2)
{
return w1 < w2 || w1 == w2;
}
bool operator>(const WrapperOne& w1, const WrapperTwo& w2)
{
return !(w1 < w2) && w1 != w2;
}
bool operator>=(const WrapperOne& w1, const WrapperTwo& w2)
{
return !(w1 < w2);
}
bool operator<(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 >= w2;
}
bool operator<=(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 > w2;
}
bool operator>(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 <= w2;
}
bool operator>=(const WrapperTwo& w2, const WrapperOne& w1)
{
return w1 < w2;
}
#endif
int main()
{
WrapperOne w1{ 1 };
WrapperTwo w2{ 2 };
std::cout << (w1 < w2) << std::endl;
std::cout << (w1 <= w2) << std::endl;
std::cout << (w1 > w2) << std::endl;
std::cout << (w1 >= w2) << std::endl;
std::cout << (w1 == w2) << std::endl;
std::cout << (w1 != w2) << std::endl;
std::cout << (w2 < w1) << std::endl;
std::cout << (w2 <= w1) << std::endl;
std::cout << (w2 > w1) << std::endl;
std::cout << (w2 >= w1) << std::endl;
std::cout << (w2 == w1) << std::endl;
std::cout << (w2 != w1) << std::endl;
}