Horizon
generate_n.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_GENERATE_N_HPP
14 #define RANGES_V3_ALGORITHM_GENERATE_N_HPP
15 
16 #include <tuple>
17 #include <utility>
18 
19 #include <range/v3/range_fwd.hpp>
20 
21 #include <range/v3/algorithm/result_types.hpp>
28 #include <range/v3/utility/static_const.hpp>
29 
30 #include <range/v3/detail/prologue.hpp>
31 
32 namespace ranges
33 {
36  template<typename O, typename F>
37  using generate_n_result = detail::out_fun_result<O, F>;
38 
39  RANGES_FUNC_BEGIN(generate_n)
40 
41 
42  template(typename O, typename F)(
43  requires invocable<F &> AND output_iterator<O, invoke_result_t<F &>>)
44  constexpr generate_n_result<O, F> //
45  RANGES_FUNC(generate_n)(O first, iter_difference_t<O> n, F fun)
46  {
47  RANGES_EXPECT(n >= 0);
48  auto norig = n;
49  auto b = uncounted(first);
50  for(; 0 != n; ++b, --n)
51  *b = invoke(fun);
52  return {recounted(first, b, norig), detail::move(fun)};
53  }
54 
55  RANGES_FUNC_END(generate_n)
56 
57  namespace cpp20
58  {
59  using ranges::generate_n;
60  using ranges::generate_n_result;
61  } // namespace cpp20
62  // @}
63 } // namespace ranges
64 
65 #include <range/v3/detail/epilogue.hpp>
66 
67 #endif
template(typename O, typename F)(requires invocable< F & > AND output_iterator< O
function template generate_n
CPP_concept invocable
\concept invocable
Definition: concepts.hpp:48
CPP_concept output_iterator
\concept output_iterator
Definition: concepts.hpp:347
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