[PATCH] D37417: Add range based wrapper around std::for_each.
Keith via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 2 18:08:47 PDT 2017
kpw created this revision.
There are a few range based wrappers llvm provides already. They're handy and I
couldn't find one for std::for_each, so I'm adding it.
https://reviews.llvm.org/D37417
Files:
include/llvm/ADT/STLExtras.h
unittests/ADT/STLExtrasTest.cpp
Index: unittests/ADT/STLExtrasTest.cpp
===================================================================
--- unittests/ADT/STLExtrasTest.cpp
+++ unittests/ADT/STLExtrasTest.cpp
@@ -318,4 +318,14 @@
EXPECT_EQ(7, V[3]);
}
+TEST(STLExtrasTest, ForEach) {
+ std::vector<std::pair<int, int>> V = {{1, 2}, {3, 4}, {5, 6}};
+ std::vector<int> Evens{};
+ auto BackInserter = std::back_inserter(Evens);
+ for_each(V,
+ [&](std::pair<int, int> Element) { BackInserter = Element.second; });
+ std::vector<int> ExpectedEvens = {2, 4, 6};
+ EXPECT_EQ(Evens, ExpectedEvens);
+}
+
}
Index: include/llvm/ADT/STLExtras.h
===================================================================
--- include/llvm/ADT/STLExtras.h
+++ include/llvm/ADT/STLExtras.h
@@ -852,6 +852,13 @@
return std::find_if_not(std::begin(Range), std::end(Range), P);
}
+/// Provide wrapper to std::for_each which take ranges instead of having to
+/// pass begin/end explicitly.
+template <typename R, typename UnaryFunction>
+UnaryFunction for_each(R &&Range, UnaryFunction F) {
+ return std::for_each(std::begin(Range), std::end(Range), F);
+}
+
/// Provide wrappers to std::remove_if which take ranges instead of having to
/// pass begin/end explicitly.
template <typename R, typename UnaryPredicate>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37417.113667.patch
Type: text/x-patch
Size: 1294 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170903/9ce22b63/attachment.bin>
More information about the llvm-commits
mailing list