Skip to content

Commit 0e876ed

Browse files
committed
[libc++] Implement operator<=> for error_category
Implements part of P1614R2 "The Mothership has Landed" Differential Revision: https://reviews.llvm.org/D131363
1 parent 24cdf97 commit 0e876ed

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

libcxx/docs/Status/SpaceshipProjects.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Section,Description,Dependencies,Assignee,Complete
1515
| `[type.info] <https://wg21.link/type.info>`_,| `typeinfo <https://reviews.llvm.org/D130853>`_,None,Adrian Vogelsgesang,|Complete|
1616
| `[coroutine.handle.compare] <https://wg21.link/coroutine.handle.compare>`_,| `coroutine_handle <https://reviews.llvm.org/D109433>`_,[comparisons.three.way],Chuanqi Xu,|Complete|
1717
| `[pairs.spec] <https://wg21.link/pairs.spec>`_,| `pair <https://reviews.llvm.org/D107721>`_,[expos.only.func],Kent Ross,|Complete|
18-
| `[syserr.errcat.nonvirtuals] <https://wg21.link/syserr.errcat.nonvirtuals>`_,| `error_category <https://reviews.llvm.org/D131363>`_,[comparisons.three.way],Adrian Vogelsgesang,|In Progress|
18+
| `[syserr.errcat.nonvirtuals] <https://wg21.link/syserr.errcat.nonvirtuals>`_,| `error_category <https://reviews.llvm.org/D131363>`_,[comparisons.three.way],Adrian Vogelsgesang,|Complete|
1919
| `[syserr.compare] <https://wg21.link/syserr.compare>`_,"| `error_code <https://reviews.llvm.org/D131371>`_
2020
| `error_condition <https://reviews.llvm.org/D131371>`_",None,Adrian Vogelsgesang,|In Progress|
2121
| `[tuple.rel] <https://wg21.link/tuple.rel>`_,| `tuple <https://reviews.llvm.org/D108250>`_,[expos.only.func],Kent Ross,|Complete|

libcxx/include/system_error

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public:
3232
virtual string message(int ev) const = 0;
3333
3434
bool operator==(const error_category& rhs) const noexcept;
35-
bool operator!=(const error_category& rhs) const noexcept;
36-
bool operator<(const error_category& rhs) const noexcept;
35+
bool operator!=(const error_category& rhs) const noexcept; // removed in C++20
36+
bool operator<(const error_category& rhs) const noexcept; // removed in C++20
37+
strong_ordering operator<=>(const error_category& rhs) const noexcept; // C++20
3738
};
3839
3940
const error_category& generic_category() noexcept;
@@ -147,6 +148,7 @@ template <> struct hash<std::error_condition>;
147148
#include <__errc>
148149
#include <__functional/hash.h>
149150
#include <__functional/unary_function.h>
151+
#include <__memory/addressof.h>
150152
#include <stdexcept>
151153
#include <string>
152154
#include <type_traits>
@@ -223,12 +225,21 @@ public:
223225
_LIBCPP_INLINE_VISIBILITY
224226
bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
225227

228+
#if _LIBCPP_STD_VER > 17
229+
230+
_LIBCPP_HIDE_FROM_ABI
231+
strong_ordering operator<=>(const error_category& __rhs) const noexcept {return compare_three_way()(this, std::addressof(__rhs));}
232+
233+
#else // _LIBCPP_STD_VER > 17
234+
226235
_LIBCPP_INLINE_VISIBILITY
227236
bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
228237

229238
_LIBCPP_INLINE_VISIBILITY
230239
bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
231240

241+
#endif // _LIBCPP_STD_VER > 17
242+
232243
friend class _LIBCPP_HIDDEN __do_message;
233244
};
234245

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// UNSUPPORTED: c++03, c++11, c++14, c++17
9+
10+
// <system_error>
11+
12+
// class error_category
13+
14+
// strong_ordering operator<=>(const error_category& rhs) const noexcept;
15+
16+
#include <system_error>
17+
#include <cassert>
18+
19+
#include "test_macros.h"
20+
#include "test_comparisons.h"
21+
22+
int main(int, char**) {
23+
AssertOrderAreNoexcept<std::error_category>();
24+
AssertOrderReturn<std::strong_ordering, std::error_category>();
25+
26+
const std::error_category& e_cat1 = std::generic_category();
27+
const std::error_category& e_cat2 = std::generic_category();
28+
const std::error_category& e_cat3 = std::system_category();
29+
30+
assert(testOrder(e_cat1, e_cat2, std::strong_ordering::equal));
31+
32+
bool isLess = e_cat1 < e_cat3;
33+
assert(testOrder(e_cat1, e_cat3, isLess ? std::strong_ordering::less : std::strong_ordering::greater));
34+
35+
return 0;
36+
}

0 commit comments

Comments
 (0)