[llvm] Reland "[STLExtras] Add a template for detecting whether a type has an equality comparison operator" (PR #176893)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 02:57:41 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Benjamin Stott (BStott6)

<details>
<summary>Changes</summary>

It seems there's a difference between Clang and MSVC's template handling, resulting in a difference in the behaviour of is_detected with private class members. [This](https://godbolt.org/z/x61YdEz44) Godbolt link demonstrates the difference. This was causing one of the test cases I made, which was checking that it didn't pick up a private equality comparison operator, to fail when compiled using MSVC.
In this PR I have just removed that test case.

---
Full diff: https://github.com/llvm/llvm-project/pull/176893.diff


2 Files Affected:

- (modified) llvm/include/llvm/ADT/STLExtras.h (+11) 
- (modified) llvm/unittests/ADT/STLExtrasTest.cpp (+29-2) 


``````````diff
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 23da931a63de1..d9c6b9ac5f1d7 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2682,6 +2682,17 @@ template <typename T> using has_sizeof = decltype(sizeof(T));
 template <typename T>
 constexpr bool is_incomplete_v = !is_detected<detail::has_sizeof, T>::value;
 
+// Detect types with equality comparison operators.
+namespace detail {
+template <typename T, typename U>
+using has_equality_comparison =
+    decltype(std::declval<const T &>() == std::declval<const U &>());
+} // namespace detail
+
+/// Detects when type `const T` can be compared for equality with `const U`.
+template <typename T, typename U = T>
+constexpr bool has_equality_comparison_v =
+    is_detected<detail::has_equality_comparison, T, U>::value;
 } // end namespace llvm
 
 namespace std {
diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp
index fe71945e4a794..af8d40575a22a 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1777,8 +1777,35 @@ TEST(STLExtrasTest, ReverseConditionally) {
 struct Foo;
 struct Bar {};
 
-static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
-static_assert(!is_incomplete_v<Bar>, "Bar is defined");
+TEST(STLExtrasTest, HasEqualityComparison) {
+  static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
+  static_assert(!is_incomplete_v<Bar>, "Bar is defined");
+
+  struct NoEqualityComparison {};
+  static_assert(!has_equality_comparison_v<NoEqualityComparison>);
+
+  // Mutating equality comparison doesn't count.
+  struct MutatingEqualityComparison {
+    bool operator==(MutatingEqualityComparison &Other) { return false; }
+  };
+  static_assert(!has_equality_comparison_v<MutatingEqualityComparison>);
+
+  struct PublicEqualityComparison {
+    bool operator==(const PublicEqualityComparison &Other) const {
+      return false;
+    }
+  };
+  static_assert(has_equality_comparison_v<PublicEqualityComparison>);
+
+  struct StructA {};
+  struct StructB {
+    bool operator==(const StructA &Other) const { return false; }
+  };
+  static_assert(!has_equality_comparison_v<StructA, StructB>);
+  static_assert(has_equality_comparison_v<StructB, StructA>);
+
+  SUCCEED();
+}
 
 TEST(STLExtrasTest, Search) {
   // Test finding a subsequence in the middle.

``````````

</details>


https://github.com/llvm/llvm-project/pull/176893


More information about the llvm-commits mailing list