[libcxx-commits] [PATCH] D131363: [libc++] Implement `operator<=>` for `error_category`

Adrian Vogelsgesang via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 7 10:15:34 PDT 2022


avogelsgesang created this revision.
avogelsgesang added reviewers: ldionne, philnik, Mordante, mumbleskates.
Herald added a project: All.
avogelsgesang requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Implements part of P1614R2 "The Mothership has Landed"


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131363

Files:
  libcxx/docs/Status/SpaceshipProjects.csv
  libcxx/include/system_error
  libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.nonvirtuals/cmp.pass.cpp


Index: libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.nonvirtuals/cmp.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/test/std/diagnostics/syserr/syserr.errcat/syserr.errcat.nonvirtuals/cmp.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <system_error>
+
+// class error_category
+
+// strong_ordering operator<=>(const error_category& rhs) const noexcept;
+
+#include <system_error>
+#include <cassert>
+
+#include "test_macros.h"
+#include "test_comparisons.h"
+
+int main(int, char**) {
+  AssertOrderAreNoexcept<std::error_category>();
+  AssertOrderReturn<std::strong_ordering, std::error_category>();
+
+  const std::error_category& e_cat1 = std::generic_category();
+  const std::error_category& e_cat2 = std::generic_category();
+  const std::error_category& e_cat3 = std::system_category();
+
+  assert(testOrder(e_cat1, e_cat2, std::strong_ordering::equal));
+
+  bool isLess = e_cat1 < e_cat3;
+  assert(testOrder(e_cat1, e_cat3, isLess ? std::strong_ordering::less : std::strong_ordering::greater));
+
+  return 0;
+}
Index: libcxx/include/system_error
===================================================================
--- libcxx/include/system_error
+++ libcxx/include/system_error
@@ -32,8 +32,9 @@
     virtual string message(int ev) const = 0;
 
     bool operator==(const error_category& rhs) const noexcept;
-    bool operator!=(const error_category& rhs) const noexcept;
-    bool operator<(const error_category& rhs) const noexcept;
+    bool operator!=(const error_category& rhs) const noexcept;              // removed in C++20
+    bool operator<(const error_category& rhs) const noexcept;               // removed in C++20
+    strong_ordering operator<=>(const error_category& rhs) const noexcept;  // C++20
 };
 
 const error_category& generic_category() noexcept;
@@ -223,12 +224,21 @@
     _LIBCPP_INLINE_VISIBILITY
     bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
 
+#if _LIBCPP_STD_VER <= 17
+
     _LIBCPP_INLINE_VISIBILITY
     bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
 
     _LIBCPP_INLINE_VISIBILITY
     bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
 
+#else // _LIBCPP_STD_VER <= 17
+
+    _LIBCPP_INLINE_VISIBILITY
+    strong_ordering operator<=>(const error_category& __rhs) const noexcept {return compare_three_way()(this, &__rhs);}
+
+#endif // _LIBCPP_STD_VER <= 17
+
     friend class _LIBCPP_HIDDEN __do_message;
 };
 
Index: libcxx/docs/Status/SpaceshipProjects.csv
===================================================================
--- libcxx/docs/Status/SpaceshipProjects.csv
+++ libcxx/docs/Status/SpaceshipProjects.csv
@@ -15,7 +15,7 @@
 | `[type.info] <https://wg21.link/type.info>`_,| `typeinfo <https://reviews.llvm.org/D130853>`_,None,Adrian Vogelsgesang,|Complete|
 | `[coroutine.handle.compare] <https://wg21.link/coroutine.handle.compare>`_,| `coroutine_handle <https://reviews.llvm.org/D109433>`_,[comparisons.three.way],Chuanqi Xu,|Complete|
 | `[pairs.spec] <https://wg21.link/pairs.spec>`_,| `pair <https://reviews.llvm.org/D107721>`_,[expos.only.func],Kent Ross,|Complete|
-| `[syserr.errcat.nonvirtuals] <https://wg21.link/syserr.errcat.nonvirtuals>`_,| error_category,[comparisons.three.way],Unassigned,|Not Started|
+| `[syserr.errcat.nonvirtuals] <https://wg21.link/syserr.errcat.nonvirtuals>`_,| `error_category <TODO>`_,[comparisons.three.way],Adrian Vogelsgesang,|Complete|
 | `[syserr.compare] <https://wg21.link/syserr.compare>`_,"| error_code
 | error_condition",None,Unassigned,|Not Started|
 | `[tuple.rel] <https://wg21.link/tuple.rel>`_,| `tuple <https://reviews.llvm.org/D108250>`_,[expos.only.func],Kent Ross,|Complete|


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131363.450647.patch
Type: text/x-patch
Size: 4203 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220807/6d5e213c/attachment.bin>


More information about the libcxx-commits mailing list