Horizon
for_each.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 #ifndef RANGES_V3_ALGORITHM_FOR_EACH_HPP
14 #define RANGES_V3_ALGORITHM_FOR_EACH_HPP
15 
16 #include <functional>
17 
18 #include <range/v3/range_fwd.hpp>
19 
20 #include <range/v3/algorithm/result_types.hpp>
29 #include <range/v3/utility/static_const.hpp>
30 
31 #include <range/v3/detail/prologue.hpp>
32 
33 namespace ranges
34 {
37  template<typename I, typename F>
38  using for_each_result = detail::in_fun_result<I, F>;
39 
40  RANGES_FUNC_BEGIN(for_each)
41 
42 
43  template(typename I, typename S, typename F, typename P = identity)(
44  requires input_iterator<I> AND sentinel_for<S, I> AND
45  indirectly_unary_invocable<F, projected<I, P>>)
46  constexpr for_each_result<I, F> RANGES_FUNC(for_each)(I first, S last, F fun, P proj = P{})
47  {
48  for(; first != last; ++first)
49  {
50  invoke(fun, invoke(proj, *first));
51  }
52  return {detail::move(first), detail::move(fun)};
53  }
54 
56  template(typename Rng, typename F, typename P = identity)(
57  requires input_range<Rng> AND
58  indirectly_unary_invocable<F, projected<iterator_t<Rng>, P>>)
59  constexpr for_each_result<borrowed_iterator_t<Rng>, F> //
60  RANGES_FUNC(for_each)(Rng && rng, F fun, P proj = P{})
61  {
62  return {(*this)(begin(rng), end(rng), ref(fun), detail::move(proj)).in,
63  detail::move(fun)};
64  }
65 
66  RANGES_FUNC_END(for_each)
67 
68  namespace cpp20
69  {
70  using ranges::for_each;
71  using ranges::for_each_result;
72  } // namespace cpp20
74 } // namespace ranges
75 
76 #include <range/v3/detail/epilogue.hpp>
77 
78 #endif
CPP_concept indirectly_unary_invocable
\concept indirectly_unary_invocable
Definition: concepts.hpp:545
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition: meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition: meta.hpp:2251
not_< empty< find< L, T > >> in
A Boolean integral constant wrapper around true if there is at least one occurrence of T in L.
Definition: meta.hpp:3081