[PATCH] D37417: Add range based wrapper around std::for_each.

Keith Wyss via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 4 13:16:25 PDT 2017


It is an equivalent concept to range based for loop. Its not strictly
necessary, but neither is std::for_each.

On Sep 4, 2017 9:20 AM, "David Blaikie" <dblaikie at gmail.com> wrote:

> What use case do you have for this over a range-based-for loop?
>
> On Sat, Sep 2, 2017 at 6:08 PM Keith via Phabricator via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> 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>
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170904/d9b67c5c/attachment.html>


More information about the llvm-commits mailing list