[libcxx-commits] [PATCH] D80904: [libcxx] adds spaceship for vector

Christopher Di Bella via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun May 31 17:49:26 PDT 2020


cjdb created this revision.
cjdb added reviewers: EricWF, mclow.lists, ldionne, jfb, BRevzin.
cjdb added a project: libc++.
Herald added subscribers: libcxx-commits, dexonsmith, yaxunl.
cjdb added a parent revision: D80902: [libcxx] adds lexicographical_compare_three_way.

[libcxx] adds spaceship for vector

- adds C++20 operator== for vector
- adds operator<=> for vector

Note: This is currently WIP due to being unable to find tests for vector's comparison operations.


Repository:
  rCXX libc++

https://reviews.llvm.org/D80904

Files:
  libcxx/include/compare
  libcxx/include/vector


Index: libcxx/include/vector
===================================================================
--- libcxx/include/vector
+++ libcxx/include/vector
@@ -249,12 +249,8 @@
 
 template <class Allocator> struct hash<std::vector<bool, Allocator>>;
 
-template <class T, class Allocator> bool operator==(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
-template <class T, class Allocator> bool operator< (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
-template <class T, class Allocator> bool operator!=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
-template <class T, class Allocator> bool operator> (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
-template <class T, class Allocator> bool operator>=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
-template <class T, class Allocator> bool operator<=(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
+template <class T, class Allocator> bool operator== (const vector<T,Allocator>& x, const vector<T,Allocator>& y);
+template <class T, class Allocator> bool operator<=>(const vector<T,Allocator>& x, const vector<T,Allocator>& y);
 
 template <class T, class Allocator>
 void swap(vector<T,Allocator>& x, vector<T,Allocator>& y)
@@ -3340,6 +3336,15 @@
     return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
 }
 
+#if !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR)
+template <class _Tp, class _Allocator>
+inline _LIBCPP_INLINE_VISIBILITY
+__synth_three_way_result<_Tp>
+operator<=>(const vector<_Tp, _Allocator>& __x, const vector<_Tp, _Allocator>& __y)
+{
+    return _VSTD::lexicographical_compare_three_way(__x.begin(), __x.end(), __y.begin(), __y.end(), __synth_three_way);
+}
+#else
 template <class _Tp, class _Allocator>
 inline _LIBCPP_INLINE_VISIBILITY
 bool
@@ -3379,6 +3384,7 @@
 {
     return !(__y < __x);
 }
+#endif // _
 
 template <class _Tp, class _Allocator>
 inline _LIBCPP_INLINE_VISIBILITY
Index: libcxx/include/compare
===================================================================
--- libcxx/include/compare
+++ libcxx/include/compare
@@ -676,6 +676,23 @@
 template<class _Tp> constexpr strong_equality strong_equal(const _Tp& __lhs, const _Tp& __rhs);
 template<class _Tp> constexpr weak_equality weak_equal(const _Tp& __lhs, const _Tp& __rhs);
 
+#if !defined(_LIBCPP_HAS_NO_SPACESHIP_OPERATOR)
+inline constexpr auto __synth_three_way = []<class T, class U>(const T& t, const U& u)
+{
+    // TODO(cjdb): add requirements once boolean-testable is committed
+    if constexpr (requires { t <=> u; }) {
+      return t <=> u;
+    } else {
+        if (t < u) return weak_ordering::less;
+        if (u < t) return weak_ordering::greater;
+        return weak_ordering::equivalent;
+    }
+};
+
+template<class T, class U=T>
+using __synth_three_way_result = decltype(__synth_three_way(declval<T&>(), declval<U&>()));
+#endif // _LIBCPP_HAS_NO_SPACESHIP_OPERATOR
+
 #endif // _LIBCPP_STD_VER > 17
 
 _LIBCPP_END_NAMESPACE_STD


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80904.267533.patch
Type: text/x-patch
Size: 3010 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200601/a745ab07/attachment.bin>


More information about the libcxx-commits mailing list