-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathop-spaceship-cpp20.cpp
More file actions
66 lines (53 loc) · 1.66 KB
/
op-spaceship-cpp20.cpp
File metadata and controls
66 lines (53 loc) · 1.66 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
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
// operator<=> (the "spaceship operator")
// auto-generates all 6 comparison operators: <, >, <=, >=, ==, !=
// from a single definition.
//
// The most important practical win:
// any type with "operator<=> = default" instantly works as a std::map key
// or in std::sort with zero extra code.
//// 1. Default impl.
// now
struct Point {
int x, y;
auto operator<=>(const Point&) const = default; // **
};
// before
struct Point_ {
int x, y;
bool operator==(const Point_& o) const { return x==o.x && y==o.y; }
bool operator< (const Point_& o) const {
return x<o.x || (x==o.x && y<o.y);
}
bool operator> (const Point_& o) const { return o < *this; }
bool operator<=(const Point_& o) const { return !(o < *this); }
bool operator>=(const Point_& o) const { return !(*this < o); }
bool operator!=(const Point_& o) const { return !(*this == o); }
};
TEST_CASE("op-space-1") {
Point a {1,2}, b {2,3};
REQUIRE(a != b);
Point_ c {1,2}, d {2,3};
REQUIRE(c != d);
}
//// 2. Custom impl. of operator<=>
struct Person {
string name;
int age;
// ** sort by age only, ignore name
auto operator<=>(const Person& rhs) const {
return age <=> rhs.age;
}
// Note: When we provide a custom operator<=>, the compiler no longer
// auto-generates ==, so we must define it explicitly
bool operator==(const Person& rhs) const {
return age == rhs.age;
}
};
TEST_CASE("op-space-2") {
Person a {"aa", 5}, b {"bb", 10};
REQUIRE(a != b);
}