Added a for_each function to iterate through ranges
Summary: Adding a for_each function that allows generalized indexed and breakable iteration through ranges, these can either be runtime ranges (i.e. entities for which std::begin and std::end work) or compile time ranges (as deemed by the presence of a std::tuple_length<>, get<> (ADL resolved) functions) The function is made to provide a convenient library based solution to the proposal p0589r0, which aims to generalize the range based for loop even further to work with compile time ranges A drawback of using range based for loops is that sometimes you do not have access to the index within the range. This provides easy access to that, even with compile time ranges. Further this also provides a good way to break out of a loop without any overhead when that is not used. A simple use case would be when using futures, if the user was doing calls to n servers then they would accept the callback with the futures like this auto vec = std::vector<std::future<int>>{request_one(), ...}; when_all(vec.begin(), vec.end()).then([](auto futures) { folly::for_each(futures, [](auto& fut) { ... }); }); Now when this code switches to use tuples instead of the runtime std::vector, then the loop does not need to change, the code will still work just fine when_all(future_one, future_two, future_three).then([](auto futures) { folly::for_each(futures, [](auto& fut) { ... }); }); Reviewed By: yfeldblum Differential Revision: D5557336 fbshipit-source-id: 79fcbafa7e1671f8856f0dcb7bf7996435dadeaa
Showing
folly/Foreach-inl.h
0 → 100644
This diff is collapsed.
Please register or sign in to comment