[libcxx-commits] [libcxx] 9df5892 - [libc++] Implement `operator<=>` for `type_index`

Adrian Vogelsgesang via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 9 16:35:24 PDT 2022


Author: Adrian Vogelsgesang
Date: 2022-08-09T16:35:17-07:00
New Revision: 9df58928045f2370caa201ba14d4ec62cba00e46

URL: https://github.com/llvm/llvm-project/commit/9df58928045f2370caa201ba14d4ec62cba00e46
DIFF: https://github.com/llvm/llvm-project/commit/9df58928045f2370caa201ba14d4ec62cba00e46.diff

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

Implements part of P1614R2 "The Mothership has Landed"

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

Added: 
    libcxx/test/std/utilities/type.index/type.index.members/cmp.pass.cpp

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

Removed: 
    libcxx/test/std/utilities/type.index/type.index.members/eq.pass.cpp
    libcxx/test/std/utilities/type.index/type.index.members/lt.pass.cpp


################################################################################
diff  --git a/libcxx/docs/Status/SpaceshipProjects.csv b/libcxx/docs/Status/SpaceshipProjects.csv
index 4ef0ca92a82b0..203ebac1b2818 100644
--- a/libcxx/docs/Status/SpaceshipProjects.csv
+++ b/libcxx/docs/Status/SpaceshipProjects.csv
@@ -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|

diff  --git a/libcxx/include/typeindex b/libcxx/include/typeindex
index 5fb7b30ecfee4..6b2a78ec8cb11 100644
--- a/libcxx/include/typeindex
+++ b/libcxx/include/typeindex
@@ -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();}

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
index 0000000000000..0892b60441476
--- /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
+//
+//===----------------------------------------------------------------------===//
+
+// <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;
+}

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
index f64e2a7cb6c83..0000000000000
--- 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
-//
-//===----------------------------------------------------------------------===//
-
-// <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;
-}

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
index 360793864048c..0000000000000
--- 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
-//
-//===----------------------------------------------------------------------===//
-
-// <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;
-}


        


More information about the libcxx-commits mailing list