Explorar o código

refactor: add support for three-way-comparison

Sam Jaffe hai 8 meses
pai
achega
63e654bfe1
Modificáronse 1 ficheiros con 21 adicións e 1 borrados
  1. 21 1
      include/opaque_typedef/comparable.hpp

+ 21 - 1
include/opaque_typedef/comparable.hpp

@@ -8,9 +8,23 @@ namespace types {
     friend bool operator!=(Self const & lhs, Self const & rhs) {
       return !(lhs == rhs);
     }
+
+#if __cplusplus >= 202002L
+    friend std::partial_ordering operator<=>(Self const & lhs,
+                                             Self const & rhs) {
+      return lhs == rhs ? std::partial_ordering::equivalent
+                        : std::partial_ordering::unordered;
+    }
+#endif
   };
 
-  template <typename Self> struct Comparable : EqualityComparable<Self> {
+  template <typename Self> struct Comparable {
+    friend bool operator==(Self const & lhs, Self const & rhs) {
+      return lhs.get() == rhs.get();
+    }
+    friend bool operator!=(Self const & lhs, Self const & rhs) {
+      return !(lhs == rhs);
+    }
     friend bool operator<(Self const & lhs, Self const & rhs) {
       return lhs.get() < rhs.get();
     }
@@ -23,5 +37,11 @@ namespace types {
     friend bool operator>=(Self const & lhs, Self const & rhs) {
       return !(lhs.get() < rhs.get());
     }
+
+#if __cplusplus >= 202002L
+    friend auto operator<=>(Self const & lhs, Self const & rhs) {
+      return lhs.get() <=> rhs.get();
+    }
+#endif
   };
 }