diff --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv --- a/libcxx/docs/Status/SpaceshipProjects.csv +++ b/libcxx/docs/Status/SpaceshipProjects.csv @@ -28,7 +28,7 @@ | `variant `_",None,Kent Ross,|In Progress| | `[unique.ptr.special] `_,| `unique_ptr `_,[comparisons.three.way],Adrian Vogelsgesang,|Complete| | `[util.smartptr.shared.cmp] `_,| `shared_ptr `_,[comparisons.three.way],Adrian Vogelsgesang,|Complete| -| `[type.index.members] `_,| `type_index `_,None,Adrian Vogelsgesang,|In Progress| +| `[type.index.members] `_,| `type_index `_,None,Adrian Vogelsgesang,|Complete| | `[charconv.syn] `_,| to_chars_result,None,Mark de Wever,|Complete| | `[charconv.syn] `_,| from_chars_result,None,Mark de Wever,|Complete| | `[stacktrace.entry.cmp] `_,| stacktrace_entry,None,Unassigned,|Not Started| diff --git a/libcxx/include/typeindex b/libcxx/include/typeindex --- a/libcxx/include/typeindex +++ b/libcxx/include/typeindex @@ -23,11 +23,12 @@ 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 @@ 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 @@ _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();} diff --git a/libcxx/test/std/utilities/type.index/type.index.members/cmp.pass.cpp b/libcxx/test/std/utilities/type.index/type.index.members/cmp.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/utilities/type.index/type.index.members/cmp.pass.cpp @@ -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 +// +//===----------------------------------------------------------------------===// + +// + +// 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 +#include + +#include "test_macros.h" +#include "test_comparisons.h" + +int main(int, char**) { + AssertComparisonsAreNoexcept(); + AssertComparisonsReturnBool(); +#if TEST_STD_VER > 17 + AssertOrderAreNoexcept(); + AssertOrderReturn(); +#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; +} diff --git a/libcxx/test/std/utilities/type.index/type.index.members/eq.pass.cpp b/libcxx/test/std/utilities/type.index/type.index.members/eq.pass.cpp deleted file mode 100644 --- a/libcxx/test/std/utilities/type.index/type.index.members/eq.pass.cpp +++ /dev/null @@ -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 -// -//===----------------------------------------------------------------------===// - -// - -// class type_index - -// bool operator==(const type_index& rhs) const; -// bool operator!=(const type_index& rhs) const; - -// UNSUPPORTED: no-rtti - -#include -#include - -#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; -} diff --git a/libcxx/test/std/utilities/type.index/type.index.members/lt.pass.cpp b/libcxx/test/std/utilities/type.index/type.index.members/lt.pass.cpp deleted file mode 100644 --- a/libcxx/test/std/utilities/type.index/type.index.members/lt.pass.cpp +++ /dev/null @@ -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 -// -//===----------------------------------------------------------------------===// - -// - -// 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 -#include - -#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; -}