[PATCH] D48632: ADT: Move ArrayRef comparison operators into the class

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 27 05:05:13 PDT 2018


labath created this revision.
labath added reviewers: zturner, dblaikie.

I don't fully understand all language nuances here, but this allows the
implicit ArrayRef conversions to kick in when e.g. comparing ArrayRef to
a SmallVector. That seems like a good thing.


Repository:
  rL LLVM

https://reviews.llvm.org/D48632

Files:
  include/llvm/ADT/ArrayRef.h
  unittests/ADT/ArrayRefTest.cpp


Index: unittests/ADT/ArrayRefTest.cpp
===================================================================
--- unittests/ADT/ArrayRefTest.cpp
+++ unittests/ADT/ArrayRefTest.cpp
@@ -188,6 +188,9 @@
   EXPECT_TRUE(AR1b.equals({3, 4, 5, 6}));
   EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6}));
   EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7}));
+
+  SmallVector<int, 8> V1{1, 2, 3, 4, 5, 6, 7, 8};
+  EXPECT_EQ(AR1, V1);
 }
 
 TEST(ArrayRefTest, EmptyEquals) {
Index: include/llvm/ADT/ArrayRef.h
===================================================================
--- include/llvm/ADT/ArrayRef.h
+++ include/llvm/ADT/ArrayRef.h
@@ -272,6 +272,16 @@
       return std::vector<T>(Data, Data+Length);
     }
 
+    /// @}
+    /// @name Comparison operators
+    /// @{
+
+    friend bool operator==(ArrayRef LHS, ArrayRef RHS) {
+      return LHS.equals(RHS);
+    }
+
+    friend bool operator!=(ArrayRef LHS, ArrayRef RHS) { return !(LHS == RHS); }
+
     /// @}
   };
 
@@ -510,22 +520,6 @@
     return MutableArrayRef<T>(data, length);
   }
 
-  /// @}
-  /// @name ArrayRef Comparison Operators
-  /// @{
-
-  template<typename T>
-  inline bool operator==(ArrayRef<T> LHS, ArrayRef<T> RHS) {
-    return LHS.equals(RHS);
-  }
-
-  template<typename T>
-  inline bool operator!=(ArrayRef<T> LHS, ArrayRef<T> RHS) {
-    return !(LHS == RHS);
-  }
-
-  /// @}
-
   // ArrayRefs can be treated like a POD type.
   template <typename T> struct isPodLike;
   template <typename T> struct isPodLike<ArrayRef<T>> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48632.153047.patch
Type: text/x-patch
Size: 1500 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180627/36d35869/attachment.bin>


More information about the llvm-commits mailing list