C++ Logo

std-proposals

Advanced search

std::ranges APIs with Execution Policy

From: JIMMY HU <s103360021_at_[hidden]>
Date: Tue, 7 Dec 2021 12:00:56 +0800
Hi,

I am trying to propose new APIs (in header <algorithm>) for integrating
std::ranges with execution policy parameters. The following description
focuses on *std::ranges::for_each* case.

I've checked the document of std::for_each (
https://en.cppreference.com/w/cpp/algorithm/for_each) and
std::ranges::for_each (
https://en.cppreference.com/w/cpp/algorithm/ranges/for_each). std::for_each
provides an overloading with ExecutionPolicy as the first argument, to
increase consistency and usability of execution policy parameters, I think
that execution policy can be added into std::ranges::for_each function and
the following overloading structure can be considered.

template< class ExecutionPolicy, std::input_iterator I,
std::sentinel_for<I> S, class Proj = std::identity,
std::indirectly_unary_invocable<std::projected<I, Proj>> Fun >requires
(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>)
constexpr for_each_result<I, Fun>
          for_each( ExecutionPolicy&& policy, I first, S last, Fun f,
Proj proj = {} );
template< class ExecutionPolicy, ranges::input_range R, class Proj =
std::identity,
std::indirectly_unary_invocable<std::projected<ranges::iterator_t<R>,
Proj>> Fun >requires
(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>)
constexpr for_each_result<ranges::borrowed_iterator_t<R>, Fun>
          for_each( ExecutionPolicy&& policy, R&& r, Fun f, Proj proj = {} );


The usage examples:

std::vector<int> nums{3, 4, 2, 8, 15, 267};
auto print = [](const auto& n) { std::cout << ' ' << n; };
namespace ranges = std::ranges;
ranges::for_each(std::execution::seq, std::as_const(nums), print); //
the usage with std::execution::sequenced_policy
ranges::for_each(std::execution::seq, std::as_const(nums), print); //
the usage with std::execution::parallel_policy

I want to hear some feedback about this idea. The formal draft of this
feature is working on. Thank you so much.

Sincerely,

-- 
Jimmy Hu
Email: s103360021_at_[hidden]

Received on 2021-12-06 22:01:21