Fix iterator_adaptor_base/enumerator_iter to allow composition of llvm::enumerate with llvm::make_filter_range

* Properly specify reference type in enumerator_iter
* Fix constness of iterator_adaptor_base::operator*

Differential Revision: https://reviews.llvm.org/D112981
This commit is contained in:
Mehdi Amini 2021-11-02 04:52:48 +00:00
parent ca0ed40e00
commit ba7a6b314f
3 changed files with 13 additions and 3 deletions

View File

@ -1907,8 +1907,7 @@ class enumerator_iter
: public iterator_facade_base<
enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
typename std::iterator_traits<IterOfRange<R>>::difference_type,
typename std::iterator_traits<IterOfRange<R>>::pointer,
typename std::iterator_traits<IterOfRange<R>>::reference> {
typename std::iterator_traits<IterOfRange<R>>::pointer> {
using result_type = result_pair<R>;
public:

View File

@ -296,7 +296,8 @@ public:
return LHS.I < RHS.I;
}
ReferenceT operator*() const { return *I; }
const ReferenceT operator*() const { return *I; }
ReferenceT operator*() { return *I; }
};
/// An iterator type that allows iterating over the pointees via some

View File

@ -178,6 +178,16 @@ TEST(FilterIteratorTest, Lambda) {
EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
}
TEST(FilterIteratorTest, Enumerate) {
auto IsOdd = [](auto N) { return N.value() % 2 == 1; };
int A[] = {0, 1, 2, 3, 4, 5, 6};
auto Enumerate = llvm::enumerate(A);
SmallVector<int> Actual;
for (auto IndexedValue : make_filter_range(Enumerate, IsOdd))
Actual.push_back(IndexedValue.value());
EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
}
TEST(FilterIteratorTest, CallableObject) {
int Counter = 0;
struct Callable {