Skip to content

Commit ba7a6b3

Browse files
committed
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
1 parent ca0ed40 commit ba7a6b3

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

llvm/include/llvm/ADT/STLExtras.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,7 @@ class enumerator_iter
19071907
: public iterator_facade_base<
19081908
enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
19091909
typename std::iterator_traits<IterOfRange<R>>::difference_type,
1910-
typename std::iterator_traits<IterOfRange<R>>::pointer,
1911-
typename std::iterator_traits<IterOfRange<R>>::reference> {
1910+
typename std::iterator_traits<IterOfRange<R>>::pointer> {
19121911
using result_type = result_pair<R>;
19131912

19141913
public:

llvm/include/llvm/ADT/iterator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ class iterator_adaptor_base
296296
return LHS.I < RHS.I;
297297
}
298298

299-
ReferenceT operator*() const { return *I; }
299+
const ReferenceT operator*() const { return *I; }
300+
ReferenceT operator*() { return *I; }
300301
};
301302

302303
/// An iterator type that allows iterating over the pointees via some

llvm/unittests/ADT/IteratorTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ TEST(FilterIteratorTest, Lambda) {
178178
EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
179179
}
180180

181+
TEST(FilterIteratorTest, Enumerate) {
182+
auto IsOdd = [](auto N) { return N.value() % 2 == 1; };
183+
int A[] = {0, 1, 2, 3, 4, 5, 6};
184+
auto Enumerate = llvm::enumerate(A);
185+
SmallVector<int> Actual;
186+
for (auto IndexedValue : make_filter_range(Enumerate, IsOdd))
187+
Actual.push_back(IndexedValue.value());
188+
EXPECT_EQ((SmallVector<int, 3>{1, 3, 5}), Actual);
189+
}
190+
181191
TEST(FilterIteratorTest, CallableObject) {
182192
int Counter = 0;
183193
struct Callable {

0 commit comments

Comments
 (0)