[llvm] c312f02 - [STLExtras] Make indexed_accessor_range operator== compatible with C++20

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Sat May 21 04:06:00 PDT 2022


Author: Benjamin Kramer
Date: 2022-05-21T13:00:30+02:00
New Revision: c312f025940d79b858166b80c50526558864d54e

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

LOG: [STLExtras] Make indexed_accessor_range operator== compatible with C++20

This would be ambigious with itself when C++20 tries to lookup the
reversed form. I didn't find a use in LLVM, but MLIR does a lot of
comparisons of ranges of different types.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/STLExtras.h
    llvm/unittests/Support/IndexedAccessorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index b186489295b41..0f713ede8b9e2 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1181,13 +1181,15 @@ class indexed_accessor_range_base {
   }
 
   /// Compare this range with another.
-  template <typename OtherT> bool operator==(const OtherT &other) const {
-    return size() ==
-               static_cast<size_t>(std::distance(other.begin(), other.end())) &&
-           std::equal(begin(), end(), other.begin());
-  }
-  template <typename OtherT> bool operator!=(const OtherT &other) const {
-    return !(*this == other);
+  template <typename OtherT>
+  friend bool operator==(const indexed_accessor_range_base &lhs,
+                         const OtherT &rhs) {
+    return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
+  }
+  template <typename OtherT>
+  friend bool operator!=(const indexed_accessor_range_base &lhs,
+                         const OtherT &rhs) {
+    return !(lhs == rhs);
   }
 
   /// Return the size of this range.

diff  --git a/llvm/unittests/Support/IndexedAccessorTest.cpp b/llvm/unittests/Support/IndexedAccessorTest.cpp
index 501d7a6ea2ec0..02b565634e2a9 100644
--- a/llvm/unittests/Support/IndexedAccessorTest.cpp
+++ b/llvm/unittests/Support/IndexedAccessorTest.cpp
@@ -46,4 +46,18 @@ TEST(AccessorRange, SliceTest) {
   compareData(range.slice(2, 3), data.slice(2, 3));
   compareData(range.slice(0, 5), data.slice(0, 5));
 }
+
+TEST(AccessorRange, EqualTest) {
+  int32_t rawData1[] = {0, 1, 2, 3, 4};
+  uint64_t rawData2[] = {0, 1, 2, 3, 4};
+
+  ArrayIndexedAccessorRange<int32_t> range1(rawData1, /*start=*/0,
+                                            /*numElements=*/5);
+  ArrayIndexedAccessorRange<uint64_t> range2(rawData2, /*start=*/0,
+                                             /*numElements=*/5);
+  EXPECT_TRUE(range1 == range2);
+  EXPECT_FALSE(range1 != range2);
+  EXPECT_TRUE(range2 == range1);
+  EXPECT_FALSE(range2 != range1);
+}
 } // end anonymous namespace


        


More information about the llvm-commits mailing list