[llvm] 44d1832 - Reland #2 "[STLExtras] Add a template for detecting whether a type has an equality comparison operator" (#177415)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 23 09:25:47 PDT 2026
Author: Benjamin Stott
Date: 2026-04-23T17:25:42+01:00
New Revision: 44d1832283ca416b30c86bad028f2c6252e805b9
URL: https://github.com/llvm/llvm-project/commit/44d1832283ca416b30c86bad028f2c6252e805b9
DIFF: https://github.com/llvm/llvm-project/commit/44d1832283ca416b30c86bad028f2c6252e805b9.diff
LOG: Reland #2 "[STLExtras] Add a template for detecting whether a type has an equality comparison operator" (#177415)
Reland PR #176893 which was an attempt to reland PR #176429.
Fix:
There was a test case asserting that for types `StructA` `StructB` where
`operator==(const StructB &, const StructA &)` is defined,
`has_equality_comparison_v<StructA, StructB>` is false because the
arguments are the wrong way around. However, in C++20, operator overload
resolution was changed so that for reflexive comparison operators `==`
and `!=` if a candidate exists with the arguments swapped then this will
be used. This means the test case failed when compiled with C++20. That
check was simply removed in this version.
Added:
Modified:
llvm/include/llvm/ADT/STLExtras.h
llvm/unittests/ADT/STLExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 80c97e77724e9..ba9c5352fd736 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -2692,6 +2692,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 f068563dae404..2f1e01d16e3c5 100644
--- a/llvm/unittests/ADT/STLExtrasTest.cpp
+++ b/llvm/unittests/ADT/STLExtrasTest.cpp
@@ -1807,6 +1807,32 @@ struct Bar {};
static_assert(is_incomplete_v<Foo>, "Foo is incomplete");
static_assert(!is_incomplete_v<Bar>, "Bar is defined");
+TEST(STLExtrasTest, HasEqualityComparison) {
+ 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<StructB, StructA>);
+
+ SUCCEED();
+}
+
TEST(STLExtrasTest, Search) {
// Test finding a subsequence in the middle.
std::vector<int> Haystack = {1, 2, 3, 4, 5, 6, 7, 8};
More information about the llvm-commits
mailing list