diff --git a/include/llvm/ADT/iterator.h b/include/llvm/ADT/iterator.h index b1d29a88623..00d0ba15edd 100644 --- a/include/llvm/ADT/iterator.h +++ b/include/llvm/ADT/iterator.h @@ -163,10 +163,9 @@ public: /// \code /// typedef pointee_iterator::iterator> iterator; /// \endcode -template < - typename WrappedIteratorT, - typename T = typename std::remove_pointer< - typename std::iterator_traits::value_type>::type> +template ())>::type> struct pointee_iterator : iterator_adaptor_base, WrappedIteratorT, T> { diff --git a/unittests/Support/IteratorTest.cpp b/unittests/Support/IteratorTest.cpp index 3a16406d6fa..83848328c0c 100644 --- a/unittests/Support/IteratorTest.cpp +++ b/unittests/Support/IteratorTest.cpp @@ -8,6 +8,7 @@ //===----------------------------------------------------------------------===// #include "llvm/ADT/iterator.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" #include "gtest/gtest.h" @@ -56,4 +57,45 @@ TEST(PointeeIteratorTest, Basic) { EXPECT_EQ(End, I); } +TEST(PointeeIteratorTest, SmartPointer) { + SmallVector, 4> V; + V.push_back(make_unique(1)); + V.push_back(make_unique(2)); + V.push_back(make_unique(3)); + V.push_back(make_unique(4)); + + typedef pointee_iterator< + SmallVectorImpl>::const_iterator> test_iterator; + + test_iterator Begin, End; + Begin = V.begin(); + End = test_iterator(V.end()); + + test_iterator I = Begin; + for (int i = 0; i < 4; ++i) { + EXPECT_EQ(*V[i], *I); + + EXPECT_EQ(I, Begin + i); + EXPECT_EQ(I, std::next(Begin, i)); + test_iterator J = Begin; + J += i; + EXPECT_EQ(I, J); + EXPECT_EQ(*V[i], Begin[i]); + + EXPECT_NE(I, End); + EXPECT_GT(End, I); + EXPECT_LT(I, End); + EXPECT_GE(I, Begin); + EXPECT_LE(Begin, I); + + EXPECT_EQ(i, I - Begin); + EXPECT_EQ(i, std::distance(Begin, I)); + EXPECT_EQ(Begin, I - i); + + test_iterator K = I++; + EXPECT_EQ(K, std::prev(I)); + } + EXPECT_EQ(End, I); +} + } // anonymous namespace