[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.
This commit is contained in:
Benjamin Kramer 2022-05-21 12:58:39 +02:00
parent 8ba1421432
commit c312f02594
2 changed files with 22 additions and 6 deletions

View File

@ -1181,13 +1181,15 @@ public:
}
/// 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>
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> 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 !(lhs == rhs);
}
/// Return the size of this range.

View File

@ -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