[libcxx-commits] [libcxx] 3bc7633 - [libc++][spaceship] Implement `operator<=>` for `forward_list`

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Fri Mar 3 04:43:20 PST 2023


Author: Hristo Hristov
Date: 2023-03-03T13:43:03+01:00
New Revision: 3bc76339ea887e903d3e72f0e973d117d9c69c35

URL: https://github.com/llvm/llvm-project/commit/3bc76339ea887e903d3e72f0e973d117d9c69c35
DIFF: https://github.com/llvm/llvm-project/commit/3bc76339ea887e903d3e72f0e973d117d9c69c35.diff

LOG: [libc++][spaceship] Implement `operator<=>` for `forward_list`

Implemented `operator<=>` for `forward_list`

Reviewed By: #libc, philnik

Spies: philnik, libcxx-commits, yaxunl

Differential Revision: https://reviews.llvm.org/D145172

Added: 
    libcxx/test/libcxx/containers/sequences/forwardlist/compare.three_way.pass.cpp

Modified: 
    libcxx/docs/Status/SpaceshipProjects.csv
    libcxx/include/forward_list

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv
index b2c8b3d63797..adf16c3548ad 100644
--- a/libcxx/docs/Status/SpaceshipProjects.csv
+++ b/libcxx/docs/Status/SpaceshipProjects.csv
@@ -37,7 +37,7 @@ Section,Description,Dependencies,Assignee,Complete
 | `[string.view.comparison] <https://wg21.link/string.view.comparison>`_,| `basic_string_view <https://reviews.llvm.org/D130295>`_,None,Mark de Wever,|Complete|
 | `[array.syn] <https://wg21.link/array.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `array <https://reviews.llvm.org/D132265>`_,[expos.only.func],Adrian Vogelsgesang,|In Progress|
 | `[deque.syn] <https://wg21.link/deque.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| deque <https://reviews.llvm.org/D144821>,[expos.only.func],Hristo Hristov,|Complete|
-| `[forward.list.syn] <https://wg21.link/forward.list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| forward_list,[expos.only.func],Unassigned,|Not Started|
+| `[forward.list.syn] <https://wg21.link/forward.list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `forward_list <https://reviews.llvm.org/D145172>`_,[expos.only.func],Hristo Hristov,|Complete|
 | `[list.syn] <https://wg21.link/list.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `list <https://reviews.llvm.org/D132312>`_,[expos.only.func],Adrian Vogelsgesang,|Complete|
 | `[vector.syn] <https://wg21.link/vector.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),| `vector <https://reviews.llvm.org/D132268>`_,[expos.only.func],Adrian Vogelsgesang,|In Progress|
 | `[associative.map.syn] <https://wg21.link/associative.map.syn>`_ (`general <https://wg21.link/container.opt.reqmts>`_),"| map

diff  --git a/libcxx/include/forward_list b/libcxx/include/forward_list
index 927637655950..f5a6120089fd 100644
--- a/libcxx/include/forward_list
+++ b/libcxx/include/forward_list
@@ -146,23 +146,27 @@ template <class T, class Allocator>
 
 template <class T, class Allocator>
     bool operator< (const forward_list<T, Allocator>& x,
-                    const forward_list<T, Allocator>& y);
+                    const forward_list<T, Allocator>& y); // removed in C++20
 
 template <class T, class Allocator>
     bool operator!=(const forward_list<T, Allocator>& x,
-                    const forward_list<T, Allocator>& y);
+                    const forward_list<T, Allocator>& y); // removed in C++20
 
 template <class T, class Allocator>
     bool operator> (const forward_list<T, Allocator>& x,
-                    const forward_list<T, Allocator>& y);
+                    const forward_list<T, Allocator>& y); // removed in C++20
 
 template <class T, class Allocator>
     bool operator>=(const forward_list<T, Allocator>& x,
-                    const forward_list<T, Allocator>& y);
+                    const forward_list<T, Allocator>& y); // removed in C++20
 
 template <class T, class Allocator>
     bool operator<=(const forward_list<T, Allocator>& x,
-                    const forward_list<T, Allocator>& y);
+                    const forward_list<T, Allocator>& y); // removed in C++20
+
+template<class T, class Allocator>
+    synth-three-way-result<T> operator<=>(const forward_list<T, Allocator>& x,
+                                          const forward_list<T, Allocator>& y); // since C++20
 
 template <class T, class Allocator>
     void swap(forward_list<T, Allocator>& x, forward_list<T, Allocator>& y)
@@ -181,6 +185,7 @@ template <class T, class Allocator, class Predicate>
 
 #include <__algorithm/comp.h>
 #include <__algorithm/lexicographical_compare.h>
+#include <__algorithm/lexicographical_compare_three_way.h>
 #include <__algorithm/min.h>
 #include <__assert> // all public C++ headers provide the assertion handler
 #include <__config>
@@ -1711,6 +1716,8 @@ bool operator==(const forward_list<_Tp, _Alloc>& __x,
     return (__ix == __ex) == (__iy == __ey);
 }
 
+#if _LIBCPP_STD_VER <= 17
+
 template <class _Tp, class _Alloc>
 inline _LIBCPP_INLINE_VISIBILITY
 bool operator!=(const forward_list<_Tp, _Alloc>& __x,
@@ -1752,6 +1759,20 @@ bool operator<=(const forward_list<_Tp, _Alloc>& __x,
     return !(__y < __x);
 }
 
+#else // #if _LIBCPP_STD_VER <= 17
+
+template<class _Tp, class _Allocator>
+inline _LIBCPP_HIDE_FROM_ABI
+__synth_three_way_result<_Tp>
+operator<=>(const forward_list<_Tp, _Allocator>& __x,
+            const forward_list<_Tp, _Allocator>& __y)
+{
+    return std::lexicographical_compare_three_way(
+        __x.begin(), __x.end(), __y.begin(), __y.end(), __synth_three_way);
+}
+
+#endif // #if _LIBCPP_STD_VER <= 17
+
 template <class _Tp, class _Alloc>
 inline _LIBCPP_INLINE_VISIBILITY
 void

diff  --git a/libcxx/test/libcxx/containers/sequences/forwardlist/compare.three_way.pass.cpp b/libcxx/test/libcxx/containers/sequences/forwardlist/compare.three_way.pass.cpp
new file mode 100644
index 000000000000..ec570c6a12e6
--- /dev/null
+++ b/libcxx/test/libcxx/containers/sequences/forwardlist/compare.three_way.pass.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// <forward_list>
+
+// template<class T, class Allocator>
+//   synth-three-way-result<T> operator<=>(const forward_list<T, Allocator>& x,
+//                                         const forward_list<T, Allocator>& y);
+
+#include <cassert>
+#include <forward_list>
+
+#include "test_container_comparisons.h"
+
+int main(int, char**) {
+  assert(test_ordered_container_spaceship<std::forward_list>());
+  // `std::forward_list` is not constexpr, so no `static_assert` test here.
+  return 0;
+}


        


More information about the libcxx-commits mailing list