[llvm] r335772 - [ADT] drop_begin: use adl_begin/adl_end. NFC.
Michael Kruse via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 27 12:39:03 PDT 2018
Author: meinersbur
Date: Wed Jun 27 12:39:03 2018
New Revision: 335772
URL: http://llvm.org/viewvc/llvm-project?rev=335772&view=rev
Log:
[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
Modified:
llvm/trunk/include/llvm/ADT/iterator_range.h
llvm/trunk/unittests/ADT/IteratorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/iterator_range.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/iterator_range.h?rev=335772&r1=335771&r2=335772&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/iterator_range.h (original)
+++ llvm/trunk/include/llvm/ADT/iterator_range.h Wed Jun 27 12:39:03 2018
@@ -59,9 +59,10 @@ template <typename T> iterator_range<T>
return iterator_range<T>(std::move(p.first), std::move(p.second));
}
-template<typename T>
-iterator_range<decltype(begin(std::declval<T>()))> drop_begin(T &&t, int n) {
- return make_range(std::next(begin(t), n), end(t));
+template <typename T>
+iterator_range<decltype(adl_begin(std::declval<T>()))> drop_begin(T &&t,
+ int n) {
+ return make_range(std::next(adl_begin(t), n), adl_end(t));
}
}
Modified: llvm/trunk/unittests/ADT/IteratorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/IteratorTest.cpp?rev=335772&r1=335771&r2=335772&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/IteratorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/IteratorTest.cpp Wed Jun 27 12:39:03 2018
@@ -387,4 +387,17 @@ TEST(RangeTest, Distance) {
EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
}
+TEST(IteratorRangeTest, DropBegin) {
+ SmallVector<int, 5> vec{0, 1, 2, 3, 4};
+
+ for (int n = 0; n < 5; ++n) {
+ int i = n;
+ for (auto &v : drop_begin(vec, n)) {
+ EXPECT_EQ(v, i);
+ i += 1;
+ }
+ EXPECT_EQ(i, 5);
+ }
+}
+
} // anonymous namespace
More information about the llvm-commits
mailing list