[libc++] Implement `operator<=>` for `type_index`

Implements part of P1614R2 "The Mothership has Landed"

Differential Revision: https://reviews.llvm.org/D131357
This commit is contained in:
Adrian Vogelsgesang 2022-08-07 08:01:04 -07:00 committed by Adrian Vogelsgesang
parent 4c2fb2e551
commit 9df5892804
5 changed files with 70 additions and 84 deletions

View File

@ -28,7 +28,7 @@ Section,Description,Dependencies,Assignee,Complete
| `variant <https://reviews.llvm.org/D131372>`_",None,Kent Ross,|In Progress|
| `[unique.ptr.special] <https://wg21.link/unique.ptr.special>`_,| `unique_ptr <https://reviews.llvm.org/D130838>`_,[comparisons.three.way],Adrian Vogelsgesang,|Complete|
| `[util.smartptr.shared.cmp] <https://wg21.link/util.smartptr.shared.cmp>`_,| `shared_ptr <https://reviews.llvm.org/D130852>`_,[comparisons.three.way],Adrian Vogelsgesang,|Complete|
| `[type.index.members] <https://wg21.link/type.index.members>`_,| `type_index <https://reviews.llvm.org/D131357>`_,None,Adrian Vogelsgesang,|In Progress|
| `[type.index.members] <https://wg21.link/type.index.members>`_,| `type_index <https://reviews.llvm.org/D131357>`_,None,Adrian Vogelsgesang,|Complete|
| `[charconv.syn] <https://wg21.link/charconv.syn>`_,| to_chars_result,None,Mark de Wever,|Complete|
| `[charconv.syn] <https://wg21.link/charconv.syn>`_,| from_chars_result,None,Mark de Wever,|Complete|
| `[stacktrace.entry.cmp] <https://wg21.link/stacktrace.entry.cmp>`_,| stacktrace_entry,None,Unassigned,|Not Started|

1 Section Description Dependencies Assignee Complete
28 | `[list.syn] <https://wg21.link/list.syn>`_ (`general <https://wg21.link/container.requirements.general#14>`_) | list [expos.only.func] Unassigned |Not Started|
29 | `[vector.syn] <https://wg21.link/vector.syn>`_ (`general <https://wg21.link/container.requirements.general#14>`_) | vector [expos.only.func] Unassigned |Not Started|
30 | `[associative.map.syn] <https://wg21.link/associative.map.syn>`_ (`general <https://wg21.link/container.requirements.general#14>`_) | map | multimap [expos.only.func] Unassigned |Not Started|
31 | `[associative.set.syn] <https://wg21.link/associative.set.syn>`_ (`general <https://wg21.link/container.requirements.general#14>`_) | multiset | set [expos.only.func] Unassigned |Not Started|
32 | `[queue.ops] <https://wg21.link/queue.ops>`_ | queue None Unassigned |Not Started|
33 | `[stack.ops] <https://wg21.link/stack.ops>`_ | stack None Unassigned |Not Started|
34 | `[reverse.iter.cmp] <https://wg21.link/reverse.iter.cmp>`_ | reverse_iterator None Mikhail Maltsev |Complete|

View File

@ -23,11 +23,12 @@ public:
type_index(const type_info& rhs) noexcept;
bool operator==(const type_index& rhs) const noexcept;
bool operator!=(const type_index& rhs) const noexcept;
bool operator!=(const type_index& rhs) const noexcept; // removed in C++20
bool operator< (const type_index& rhs) const noexcept;
bool operator<=(const type_index& rhs) const noexcept;
bool operator> (const type_index& rhs) const noexcept;
bool operator>=(const type_index& rhs) const noexcept;
strong_ordering operator<=>(const type_index& rhs) const noexcept; // C++20
size_t hash_code() const noexcept;
const char* name() const noexcept;
@ -76,8 +77,10 @@ public:
bool operator==(const type_index& __y) const _NOEXCEPT
{return *__t_ == *__y.__t_;}
_LIBCPP_INLINE_VISIBILITY
#if _LIBCPP_STD_VER <= 17
bool operator!=(const type_index& __y) const _NOEXCEPT
{return *__t_ != *__y.__t_;}
#endif
_LIBCPP_INLINE_VISIBILITY
bool operator< (const type_index& __y) const _NOEXCEPT
{return __t_->before(*__y.__t_);}
@ -90,6 +93,16 @@ public:
_LIBCPP_INLINE_VISIBILITY
bool operator>=(const type_index& __y) const _NOEXCEPT
{return !__t_->before(*__y.__t_);}
#if _LIBCPP_STD_VER > 17
_LIBCPP_HIDE_FROM_ABI
strong_ordering operator<=>(const type_index& __y) const noexcept {
if (*__t_ == *__y.__t_)
return strong_ordering::equal;
if (__t_->before(*__y.__t_))
return strong_ordering::less;
return strong_ordering::greater;
}
#endif
_LIBCPP_INLINE_VISIBILITY
size_t hash_code() const _NOEXCEPT {return __t_->hash_code();}

View File

@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <typeindex>
// class type_index
// bool operator==(const type_index& rhs) const noexcept;
// bool operator!=(const type_index& rhs) const noexcept;
// bool operator< (const type_index& rhs) const noexcept;
// bool operator<=(const type_index& rhs) const noexcept;
// bool operator> (const type_index& rhs) const noexcept;
// bool operator>=(const type_index& rhs) const noexcept;
// strong_ordering operator<=>(const type_index& rhs) const noexcept;
// UNSUPPORTED: no-rtti
#include <typeindex>
#include <cassert>
#include "test_macros.h"
#include "test_comparisons.h"
int main(int, char**) {
AssertComparisonsAreNoexcept<std::type_index>();
AssertComparisonsReturnBool<std::type_index>();
#if TEST_STD_VER > 17
AssertOrderAreNoexcept<std::type_index>();
AssertOrderReturn<std::strong_ordering, std::type_index>();
#endif
std::type_index t1 = typeid(int);
std::type_index t2 = typeid(int);
std::type_index t3 = typeid(long);
// Test `t1` and `t2` which should compare equal
assert(testComparisons(t1, t2, /*isEqual*/ true, /*isLess*/ false));
#if TEST_STD_VER > 17
assert(testOrder(t1, t2, std::strong_ordering::equal));
#endif
// Test `t1` and `t3` which are not equal
bool is_less = t1 < t3;
assert(testComparisons(t1, t3, /*isEqual*/ false, is_less));
#if TEST_STD_VER > 17
assert(testOrder(t1, t3, is_less ? std::strong_ordering::less : std::strong_ordering::greater));
#endif
return 0;
}

View File

@ -1,32 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <typeindex>
// class type_index
// bool operator==(const type_index& rhs) const;
// bool operator!=(const type_index& rhs) const;
// UNSUPPORTED: no-rtti
#include <typeindex>
#include <cassert>
#include "test_macros.h"
int main(int, char**)
{
std::type_index t1 = typeid(int);
std::type_index t2 = typeid(int);
std::type_index t3 = typeid(long);
assert(t1 == t2);
assert(t1 != t3);
return 0;
}

View File

@ -1,50 +0,0 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <typeindex>
// class type_index
// bool operator< (const type_index& rhs) const;
// bool operator<=(const type_index& rhs) const;
// bool operator> (const type_index& rhs) const;
// bool operator>=(const type_index& rhs) const;
// UNSUPPORTED: no-rtti
#include <typeindex>
#include <cassert>
#include "test_macros.h"
int main(int, char**)
{
std::type_index t1 = typeid(int);
std::type_index t2 = typeid(int);
std::type_index t3 = typeid(long);
assert(!(t1 < t2));
assert( (t1 <= t2));
assert(!(t1 > t2));
assert( (t1 >= t2));
if (t1 < t3)
{
assert( (t1 < t3));
assert( (t1 <= t3));
assert(!(t1 > t3));
assert(!(t1 >= t3));
}
else
{
assert(!(t1 < t3));
assert(!(t1 <= t3));
assert( (t1 > t3));
assert( (t1 >= t3));
}
return 0;
}