Index: include/llvm/ADT/iterator.h =================================================================== --- include/llvm/ADT/iterator.h +++ include/llvm/ADT/iterator.h @@ -256,6 +256,23 @@ T &operator*() const { return **this->I; } }; +template ())> +class pointer_iterator + : public iterator_adaptor_base, + WrappedIteratorT, T> { + mutable T Ptr; + +public: + pointer_iterator() {} + + explicit pointer_iterator(WrappedIteratorT u) + : pointer_iterator::iterator_adaptor_base(std::move(u)) {} + + T &operator*() { return Ptr = &*this->I; } + const T &operator*() const { return Ptr = &*this->I; } +}; + } #endif Index: unittests/Support/IteratorTest.cpp =================================================================== --- unittests/Support/IteratorTest.cpp +++ unittests/Support/IteratorTest.cpp @@ -185,4 +185,28 @@ EXPECT_EQ((SmallVector{1, 3, 5}), Actual); } +TEST(PointerIterator, Basic) { + int A[] = {1, 2, 3, 4}; + pointer_iterator Begin(std::begin(A)), End(std::end(A)); + EXPECT_EQ(A, *Begin); + ++Begin; + EXPECT_EQ(A + 1, *Begin); + ++Begin; + EXPECT_EQ(A + 2, *Begin); + ++Begin; + EXPECT_EQ(A + 3, *Begin); + ++Begin; + EXPECT_EQ(Begin, End); +} + +TEST(PointerIterator, Const) { + int A[] = {1, 2, 3, 4}; + const pointer_iterator Begin(std::begin(A)); + EXPECT_EQ(A, *Begin); + EXPECT_EQ(A + 1, std::next(*Begin, 1)); + EXPECT_EQ(A + 2, std::next(*Begin, 2)); + EXPECT_EQ(A + 3, std::next(*Begin, 3)); + EXPECT_EQ(A + 4, std::next(*Begin, 4)); +} + } // anonymous namespace