Skip to content

Commit c312f02

Browse files
committed
[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.
1 parent 8ba1421 commit c312f02

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

llvm/include/llvm/ADT/STLExtras.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -1181,13 +1181,15 @@ class indexed_accessor_range_base {
11811181
}
11821182

11831183
/// Compare this range with another.
1184-
template <typename OtherT> bool operator==(const OtherT &other) const {
1185-
return size() ==
1186-
static_cast<size_t>(std::distance(other.begin(), other.end())) &&
1187-
std::equal(begin(), end(), other.begin());
1188-
}
1189-
template <typename OtherT> bool operator!=(const OtherT &other) const {
1190-
return !(*this == other);
1184+
template <typename OtherT>
1185+
friend bool operator==(const indexed_accessor_range_base &lhs,
1186+
const OtherT &rhs) {
1187+
return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
1188+
}
1189+
template <typename OtherT>
1190+
friend bool operator!=(const indexed_accessor_range_base &lhs,
1191+
const OtherT &rhs) {
1192+
return !(lhs == rhs);
11911193
}
11921194

11931195
/// Return the size of this range.

llvm/unittests/Support/IndexedAccessorTest.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,18 @@ TEST(AccessorRange, SliceTest) {
4646
compareData(range.slice(2, 3), data.slice(2, 3));
4747
compareData(range.slice(0, 5), data.slice(0, 5));
4848
}
49+
50+
TEST(AccessorRange, EqualTest) {
51+
int32_t rawData1[] = {0, 1, 2, 3, 4};
52+
uint64_t rawData2[] = {0, 1, 2, 3, 4};
53+
54+
ArrayIndexedAccessorRange<int32_t> range1(rawData1, /*start=*/0,
55+
/*numElements=*/5);
56+
ArrayIndexedAccessorRange<uint64_t> range2(rawData2, /*start=*/0,
57+
/*numElements=*/5);
58+
EXPECT_TRUE(range1 == range2);
59+
EXPECT_FALSE(range1 != range2);
60+
EXPECT_TRUE(range2 == range1);
61+
EXPECT_FALSE(range2 != range1);
62+
}
4963
} // end anonymous namespace

0 commit comments

Comments
 (0)