Skip to content

Commit 2cb2199

Browse files
committedJun 27, 2018
[ADT] drop_begin: use adl_begin/adl_end. NFC.
Summary: The instantiation of the drop_begin function template usually fails because the functions begin() and end() do not exist. Only when using on a container from the std namespace (or `llvm::iterator_range`s of something derived from `std::iterator`), they are matched to std::begin() and std::end() due to Koenig-lookup. Explicitly use llvm::adl_begin and llvm::adl_end to make drop_begin applicable to anything iterable (including C-style arrays). A solution for general `llvm::iterator_range`s was already tried in r244620, but got reverted in r244621 due to MSVC not liking it. Reviewers: dblaikie, grosbach, aaron.ballman, ruiu Reviewed By: dblaikie, aaron.ballman Subscribers: aaron.ballman, llvm-commits Differential Revision: https://reviews.llvm.org/D48598 llvm-svn: 335772
1 parent 5dc371a commit 2cb2199

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
 

‎llvm/include/llvm/ADT/iterator_range.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ template <typename T> iterator_range<T> make_range(std::pair<T, T> p) {
5959
return iterator_range<T>(std::move(p.first), std::move(p.second));
6060
}
6161

62-
template<typename T>
63-
iterator_range<decltype(begin(std::declval<T>()))> drop_begin(T &&t, int n) {
64-
return make_range(std::next(begin(t), n), end(t));
62+
template <typename T>
63+
iterator_range<decltype(adl_begin(std::declval<T>()))> drop_begin(T &&t,
64+
int n) {
65+
return make_range(std::next(adl_begin(t), n), adl_end(t));
6566
}
6667
}
6768

‎llvm/unittests/ADT/IteratorTest.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,17 @@ TEST(RangeTest, Distance) {
387387
EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
388388
}
389389

390+
TEST(IteratorRangeTest, DropBegin) {
391+
SmallVector<int, 5> vec{0, 1, 2, 3, 4};
392+
393+
for (int n = 0; n < 5; ++n) {
394+
int i = n;
395+
for (auto &v : drop_begin(vec, n)) {
396+
EXPECT_EQ(v, i);
397+
i += 1;
398+
}
399+
EXPECT_EQ(i, 5);
400+
}
401+
}
402+
390403
} // anonymous namespace

0 commit comments

Comments
 (0)
Please sign in to comment.