Horizon
for_each_n.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2014-present
5 // Copyright Rostislav Khlebnikov 2017
6 //
7 // Use, modification and distribution is subject to the
8 // Boost Software License, Version 1.0. (See accompanying
9 // file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 //
12 // Project home: https://github.com/ericniebler/range-v3
13 //
14 #ifndef RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
15 #define RANGES_V3_ALGORITHM_FOR_EACH_N_HPP
16 
17 #include <functional>
18 
19 #include <range/v3/range_fwd.hpp>
20 
21 #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  RANGES_FUNC_BEGIN(for_each_n)
38 
39 
40  template(typename I, typename F, typename P = identity)(
41  requires input_iterator<I> AND
42  indirectly_unary_invocable<F, projected<I, P>>)
43  constexpr I RANGES_FUNC(for_each_n)(I first, iter_difference_t<I> n, F fun, P proj = P{})
44  {
45  RANGES_EXPECT(0 <= n);
46  auto norig = n;
47  auto b = uncounted(first);
48  for(; 0 < n; ++b, --n)
49  invoke(fun, invoke(proj, *b));
50  return recounted(first, b, norig);
51  }
52 
54  template(typename Rng, typename F, typename P = identity)(
55  requires input_range<Rng> AND
56  indirectly_unary_invocable<F, projected<iterator_t<Rng>, P>>)
57  constexpr borrowed_iterator_t<Rng> RANGES_FUNC(for_each_n)(
58  Rng && rng, range_difference_t<Rng> n, F fun, P proj = P{})
59  {
60  if(sized_range<Rng>)
61  RANGES_EXPECT(n <= distance(rng));
62 
63  return (*this)(begin(rng), n, detail::move(fun), detail::move(proj));
64  }
65 
66  RANGES_FUNC_END(for_each_n)
67 
68  namespace cpp20
69  {
70  using ranges::for_each_n;
71  }
73 } // namespace ranges
74 
75 #include <range/v3/detail/epilogue.hpp>
76 
77 #endif
template(typename I, typename F, typename P=identity)(requires input_iterator< I > AND indirectly_unary_invocable< F
function template for_each_n
CPP_concept indirectly_unary_invocable
\concept indirectly_unary_invocable
Definition: concepts.hpp:545
CPP_concept input_iterator
\concept input_iterator
Definition: concepts.hpp:362
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
Definition: identity.hpp:25