Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
iota.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::iota"
2
3// Copyright 2026, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_IOTA_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_IOTA_HPP_
9
10// IWYU pragma: private, include "arene/base/algorithm.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16#include "arene/base/algorithm/detail/traits.hpp"
19#include "arene/base/stdlib_choice/declval.hpp"
20#include "arene/base/stdlib_choice/enable_if.hpp"
21#include "arene/base/stdlib_choice/is_convertible.hpp"
24// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
25
26// IWYU pragma: no_include "arene/base/stdlib_choice/iterator_traits.hpp"
27
28// parasoft-begin-suppress AUTOSAR-M2_10_1-a-2 "Similar names permitted by M2-10-1 Permit #1"
29
30namespace arene {
31namespace base {
32
33namespace iota_detail {
34
35/// @brief function object providing the implementation of 'iota'
36class iota_impl_fn {
37 public:
38 /// @brief fills a range with successive increments of the starting value
39 /// @tparam ForwardIt the type of the ForwardIt
40 /// @tparam Value the type of the value
41 /// @param first ForwardIt to the beginning of the range
42 /// @param last ForwardIt to one past the end of the range
43 /// @param value initial value to fill with
44 ///
45 /// Private implementation shared by @c arene::base::iota and @c std::iota.
46 /// This function takes arguments by lvalue reference to avoid requiring
47 /// @c Value to be movable.
48 ///
49 template <
50 class ForwardIt,
51 class Value,
55 decltype(++std::declval<Value&>())> = nullptr>
56 constexpr auto operator()(ForwardIt& first, ForwardIt& last, Value& value) const noexcept( //
58 noexcept(*std::declval<ForwardIt&>()++ = std::declval<Value&>()) && //
59 noexcept(++std::declval<Value&>()) //
60 ) //
61 -> void //
62 {
63 while (first != last) {
64 // parasoft-begin-suppress AUTOSAR-M5_2_10-a "idiomatic iterator operations permitted by M5-2-10 Permit #1"
65 *first++ = value;
66 // parasoft-end-suppress AUTOSAR-M5_2_10-a
67
68 // parasoft-begin-suppress AUTOSAR-M4_5_3-a "This function must work with all supported incrementable types"
69 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "'value' is required to be an incrementable type
70 ++value;
71 // parasoft-end-suppress AUTOSAR-M4_5_3-a
72 // parasoft-end-suppress AUTOSAR-M5_0_15-a
73 }
74 }
75};
76
77/// @brief function object implementing 'iota'
78class iota_fn {
79 public:
80 /// @brief fills a range with successive increments of the starting value
81 /// @tparam ForwardIt the type of the ForwardIt
82 /// @tparam Value the type of the value
83 /// @param first ForwardIt to the beginning of the range
84 /// @param last ForwardIt to one past the end of the range
85 /// @param value initial value to fill with
86 ///
87 /// Fills the range <c> [first, last) </c> with sequentially increasing
88 /// values, starting with @c value and repetitively evaluating
89 /// <c> ++value </c>.
90 ///
91 /// @pre @c ForwardIt must satisfy the forward iterator requirements
92 /// @pre @c T is convertible to the value type of @c ForwardIt
93 /// @pre the expression <c> ++value </c> is well-formed
94 ///
95 /// @note Complexity <br>
96 /// Exactly <c> distance(first, last) </c> increments and assignments.
97 ///
98 template <
99 class ForwardIt,
100 class Value,
104 decltype(++std::declval<Value&>())> = nullptr>
105 constexpr auto operator()(ForwardIt first, ForwardIt last, Value value) const noexcept( //
107 ) //
108 -> void //
109 {
110 iota_impl_fn{}(first, last, value);
111 }
112};
113} // namespace iota_detail
114
115/// @brief fills a range with successive increments of the starting value
116/// Assigns the starting value to the first element and increments it for each subsequent element.
117// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
118// object used in multiple TUs."
119// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
120// object used in multiple TUs."
121ARENE_CPP14_INLINE_VARIABLE(iota_detail::iota_fn, iota);
122// parasoft-end-suppress AUTOSAR-M7_3_3-a
123// parasoft-end-suppress CERT_CPP-DCL59-a
124
125} // namespace base
126} // namespace arene
127
128#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ALGORITHM_IOTA_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition accumulate.hpp:37
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...