Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
begin_end.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
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_RANGES_BEGIN_END_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RANGES_BEGIN_END_HPP_
9
10// IWYU pragma: private, include "arene/base/ranges.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17#include "arene/base/ranges/detail/is_input_or_output_iterator.hpp"
18#include "arene/base/ranges/detail/is_sentinel_for.hpp"
20#include "arene/base/stdlib_choice/cstddef.hpp"
21#include "arene/base/stdlib_choice/declval.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/extent.hpp"
24#include "arene/base/stdlib_choice/forward.hpp"
25#include "arene/base/stdlib_choice/is_array.hpp"
26#include "arene/base/stdlib_choice/is_constructible.hpp"
27#include "arene/base/stdlib_choice/is_lvalue_reference.hpp"
28#include "arene/base/stdlib_choice/is_same.hpp"
29#include "arene/base/stdlib_choice/remove_cv.hpp"
30#include "arene/base/stdlib_choice/remove_extent.hpp"
31#include "arene/base/stdlib_choice/remove_reference.hpp"
32#include "arene/base/stdlib_choice/tuple.hpp" // IWYU pragma: keep (for std::get)
33#include "arene/base/stdlib_choice/tuple_size.hpp"
36// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
37
38namespace arene {
39namespace base {
40namespace ranges {
41
42namespace begin_end_detail {
43
44/// @brief The priority of ADL overload for @c begin and @c end .
45using adl_priority_tag = priority_tag<0U>;
46/// @brief The priority of member overload for @c begin and @c end .
47using member_priority_tag = priority_tag<1U>;
48/// @brief The priority of @c std::get overload for @c begin and @c end .
49using std_array_priority_tag = priority_tag<2U>;
50/// @brief The priority of size-zero tuple-like overload for @c begin and @c end .
51using size_zero_tuple_like_priority_tag = priority_tag<3U>;
52/// @brief The priority of C-array overload for @c begin and @c end .
53using c_array_priority_tag = priority_tag<4U>;
54
55/// @brief A helper that invokes the @c impl member function of a given policy, forwarding the range and starting ptag
56template <typename Policy>
57class accessor_fn {
58 public:
59 /// @brief Returns an iterator to the input range using the specified policy's implementation.
60 ///
61 /// @tparam Rng The range type. Must satisfy either @c std::is_lvalue_reference<Rng> or
62 /// @c enable_borrowed_range_v<Rng>.
63 /// @param rng The range instance.
64 /// @return An iterator to the beginning or end of the range, as determined by the specified policy's implementation.
65 template <
66 typename Rng,
69 ARENE_NODISCARD constexpr auto operator()(Rng&& rng) const
70 noexcept(noexcept(Policy::impl(std::forward<Rng>(rng), c_array_priority_tag{})))
71 -> decltype(Policy::impl(std::forward<Rng>(rng), c_array_priority_tag{})) {
72 return Policy::impl(std::forward<Rng>(rng), c_array_priority_tag{});
73 }
74};
75
76/// @brief The implementation policy for @c begin .
77/// @see arene::base::ranges::begin
78// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
79class begin_policy {
80 public:
81 /// @brief Returns a pointer to the first element of a C-style array.
82 ///
83 /// @tparam Rng The array type.
84 /// @param rng The array instance.
85 /// @return std::remove_extent_t<Rng>* Pointer to the first element of the array.
86 template <typename Rng, constraints<std::enable_if_t<std::is_array<std::remove_reference_t<Rng>>::value>> = nullptr>
87 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, c_array_priority_tag) noexcept
89 return &std::forward<Rng>(rng)[0U];
90 }
91
92 /// @brief Returns an iterator to the beginning of a tuple-like range with zero size.
93 ///
94 /// @tparam Rng The tuple-like range type.
95 /// @tparam Itr The iterator type, deduced from the range's begin() member function.
96 /// @return Itr An iterator to the beginning of the range, constructed from nullptr.
97 /// @note This overload lets us work around the fact that @c std::array::begin isn't constexpr in C++14.
98 template <
99 typename Rng,
100 typename Itr = decltype(std::declval<Rng&&>().begin()),
102 std::enable_if_t<std::tuple_size<remove_cvref_t<Rng>>::value == 0>,
104 ARENE_NODISCARD static constexpr auto impl(Rng&&, size_zero_tuple_like_priority_tag) noexcept -> Itr {
105 return Itr{nullptr};
106 }
107
108 /// @brief Returns an iterator to the beginning of a tuple-like range with non-zero size.
109 ///
110 /// @tparam Rng The tuple-like range type. @c BeginItr and @c GetItr must be the same type.
111 /// @tparam BeginItr The iterator type, deduced from the range's begin() member function.
112 /// @tparam GetItr The iterator type, deduced from the address of the first element of the range.
113 /// @param rng The range instance.
114 /// @return GetItr An iterator to the beginning of the range, obtained from the address of the first element.
115 /// @note This overload lets us work around the fact that @c std::array::begin isn't constexpr in C++14.
116 template <
117 typename Rng,
118 typename BeginItr = decltype(std::declval<Rng&&>().begin()),
119 typename GetItr = decltype(&std::get<0>(std::declval<Rng&&>())),
122 std::enable_if_t<std::is_same<BeginItr, GetItr>::value>> = nullptr>
123 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, std_array_priority_tag) noexcept -> GetItr {
124 return &std::get<0>(std::forward<Rng>(rng));
125 }
126
127 /// @brief Returns an iterator to the beginning of a range using its member begin() member function.
128 ///
129 /// @tparam Rng The range type. Must have a member begin() member function.
130 /// @tparam Itr The type of the iterator to the beginning of the range, deduced from @c rng.begin() .
131 /// @param rng The range instance.
132 /// @return Itr An iterator to the beginning of the range, obtained from the member begin() member function.
133 template <
134 typename Rng,
135 typename Itr = decltype(std::forward<Rng>(std::declval<Rng&&>()).begin()),
136 constraints<std::enable_if_t<detail::is_input_or_output_iterator_v<Itr>>> = nullptr>
137 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, member_priority_tag) noexcept(
138 noexcept(std::forward<Rng>(std::declval<Rng&&>()).begin())
139 ) -> Itr {
140 return std::forward<Rng>(rng).begin();
141 }
142
143 /// @brief Returns an iterator to the beginning of a range using argument-dependent lookup (ADL)
144 ///
145 /// @tparam Rng The range type. @c begin(rng) must be a valid expression.
146 /// @tparam Itr The type of the iterator to the beginning of the range, deduced from @c begin(rng) .
147 /// @param rng The range instance.
148 /// @return Itr An iterator to the beginning of the range, obtained via an unqualified invocation of @c begin(rng) .
149 template <
150 typename Rng,
151 typename Itr = decltype(begin(std::forward<Rng>(std::declval<Rng&&>()))),
152 constraints<std::enable_if_t<detail::is_input_or_output_iterator_v<Itr>>> = nullptr>
153 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, adl_priority_tag) noexcept(
155 ) -> Itr {
156 return begin(std::forward<Rng>(rng));
157 }
158};
159
160} // namespace begin_end_detail
161
162/// @brief returns an iterator to the beginning of a range
163/// @tparam Rng The type of the range to get the beginning of. Must satisfy either @c std::is_lvalue_reference<Rng> or
164/// @c enable_borrowed_range_v<Rng>.
165/// The range to inspect is passed as @c rng.
166/// @return An iterator to the beginning of the range
167/// This operation is expression-equivalent to one of the following operations, in order of preference:
168/// 1. If @c Rng satisfies @c std::is_array<Rng> , returns @c &rng[0] .
169/// 2. If @c rng.begin() is well-formed, returns @c rng.begin() .
170/// 3. Otherwise, if @c begin(rng) is well-formed, returns @c begin(rng) .
171///
172/// If none of the requirements are satisfied, the function does not participate in overload resolution.
173// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
174// object used in multiple TUs."
175// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
176// object used in multiple TUs."
177// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: the referenced constexpr variable is initialized"
178ARENE_CPP14_INLINE_VARIABLE(begin_end_detail::accessor_fn<begin_end_detail::begin_policy>, begin);
179// parasoft-end-suppress CERT_CPP-DCL56-a
180// parasoft-end-suppress AUTOSAR-M7_3_3-a
181// parasoft-end-suppress CERT_CPP-DCL59-a
182
183/// @brief The type of the iterator for a given range.
184/// @tparam Rng The type of the range to extract the iterator for.
185template <typename Rng>
186using iterator_t = decltype(::arene::base::ranges::begin(std::declval<Rng&>()));
187
188namespace begin_end_detail {
189
190/// @brief The implementation policy for @c end .
191/// @see arene::base::ranges::end
192class end_policy {
193 public:
194 /// @brief Returns a pointer to one-past-the-last element of a C-style array.
195 ///
196 /// @tparam Rng The array type.
197 /// @param rng The array instance.
198 /// @return std::remove_extent_t<Rng>* Pointer to one-past-the-last element of the array.
199 template <typename Rng, constraints<std::enable_if_t<std::is_array<std::remove_reference_t<Rng>>::value>> = nullptr>
200 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, c_array_priority_tag) noexcept
203 }
204
205 /// @brief Returns an iterator to the end of a tuple-like range with zero size.
206 ///
207 /// @tparam Rng The tuple-like range type.
208 /// @tparam Itr The iterator type, deduced from the range's end() member function.
209 /// @return Itr An iterator to the end of the range, constructed from nullptr.
210 /// @note This overload lets us work around the fact that @c std::array::end isn't constexpr in C++14.
211 /// constexpr.
212 template <
213 typename Rng,
214 typename Itr = decltype(std::declval<Rng&&>().end()),
218 ARENE_NODISCARD static constexpr auto impl(Rng&&, size_zero_tuple_like_priority_tag) noexcept -> Itr {
219 return Itr{nullptr};
220 }
221
222 /// @brief Returns an iterator to the end of a tuple-like range with non-zero size.
223 ///
224 /// @tparam Rng The tuple-like range type. @c BeginItr and @c GetItr must be the same type.
225 /// @tparam BeginItr The iterator type, deduced from the range's end() member function.
226 /// @tparam GetItr The iterator type, deduced from the address of the first element of the range.
227 /// @param rng The range instance.
228 /// @return GetItr An iterator to the end of the range, obtained from the address of the first element plus the size
229 /// of the range.
230 /// @note This overload lets us work around the fact that @c std::array::end isn't constexpr in C++14.
231 template <
232 typename Rng,
233 typename BeginItr = decltype(std::declval<Rng&&>().end()),
234 typename GetItr = decltype(&std::get<0>(std::declval<Rng&&>())),
238 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, std_array_priority_tag) noexcept -> BeginItr {
239 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "False positive: the pointer points to the first element of a
240 // contiguous tuple-like range"
242 // parasoft-end-suppress AUTOSAR-M5_0_15-a
243 }
244
245 /// @brief Returns an iterator to the end of a range using its member end() member function.
246 ///
247 /// @tparam Rng The range type. Must have a member end() member function.
248 /// @tparam Sentinel The type of the sentinel (end iterator) for the range, deduced from @c rng.end() .
249 /// @param rng The range instance.
250 /// @return Sentinel The sentinel to the end of the range, obtained from the member end() member function.
251 template <
252 typename Rng,
253 typename Sentinel = decltype(std::forward<Rng>(std::declval<Rng&&>()).end()),
254 constraints<std::enable_if_t<detail::is_sentinel_for_v<Sentinel, iterator_t<Rng>>>> = nullptr>
255 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, member_priority_tag) noexcept(
256 noexcept(std::forward<Rng>(std::declval<Rng&&>()).end())
257 ) -> Sentinel {
258 return std::forward<Rng>(rng).end();
259 }
260
261 /// @brief Returns an iterator to the end of a range using argument-dependent lookup (ADL)
262 ///
263 /// @tparam Rng The range type. @c end(rng) must be a valid expression.
264 /// @tparam Sentinel The type of the sentinel (end iterator) for the range, deduced from @c end(rng).
265 /// @param rng The range instance.
266 /// @return Sentinel The sentinel to the end of the range, obtained via an unqualified invocation of @c end(rng) .
267 template <
268 typename Rng,
269 typename Sentinel = decltype(end(std::forward<Rng>(std::declval<Rng&&>()))),
270 constraints<std::enable_if_t<detail::is_sentinel_for_v<Sentinel, iterator_t<Rng>>>> = nullptr>
271 ARENE_NODISCARD static constexpr auto impl(Rng&& rng, adl_priority_tag) noexcept(
272 noexcept(end(std::forward<Rng>(std::declval<Rng&&>())))
273 ) -> Sentinel {
274 return end(std::forward<Rng>(rng));
275 }
276};
277
278// parasoft-end-suppress CERT_C-EXP37-a
279
280} // namespace begin_end_detail
281
282/// @brief returns an iterator to the end of a range
283/// @tparam Rng The type of the range to get the end of. Must satisfy either @c std::is_lvalue_reference<Rng> or
284/// @c enable_borrowed_range_v<Rng>.
285/// The range to inspect is passed as @c rng.
286/// @return An iterator to the end of the range
287/// This operation is expression-equivalent to one of the following operations, in order of preference:
288/// 1. If @c Rng satisfies @c std::is_array<Rng> , returns @c &rng[std::extent<Rng>::value] .
289/// 2. If @c rng.end() is well-formed, returns @c rng.end() .
290/// 3. Otherwise, if @c end(rng) is well-formed, returns @c end(rng) .
291///
292/// If none of the requirements are satisfied, the function does not participate in overload resolution.
293// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
294// object used in multiple TUs."
295// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
296// object used in multiple TUs."
297// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: the referenced constexpr variable is initialized"
298ARENE_CPP14_INLINE_VARIABLE(begin_end_detail::accessor_fn<begin_end_detail::end_policy>, end);
299// parasoft-end-suppress CERT_CPP-DCL56-a
300// parasoft-end-suppress AUTOSAR-M7_3_3-a
301// parasoft-end-suppress CERT_CPP-DCL59-a
302
303/// @brief The type of the sentinel (end iterator) for a given range.
304/// @tparam Rng The type of the range to extract the sentinel for.
305template <typename Rng>
306using sentinel_t = decltype(::arene::base::ranges::end(std::declval<Rng&>()));
307
308} // namespace ranges
309} // namespace base
310} // namespace arene
311
312#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RANGES_BEGIN_END_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
obtains the size of an array type along a specified dimension
Definition extent.hpp:89
static constexpr value_type value
Definition integral_constant.hpp:34
obtain the number of elements in a tuple-like type
Definition tuple_size.hpp:22
Definition external_vector.hpp:803
typedef iterator_t
The type of the iterator for a given range.
Definition begin_end.hpp:186
typedef sentinel_t
The type of the sentinel (end iterator) for a given range.
Definition begin_end.hpp:306
constexpr bool enable_borrowed_range_v
Query if a range type has opted into being a borrowed range.
Definition accumulate.hpp:37
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
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Backport of std::remove_cvref_t.
Definition remove_cvref.hpp:35
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,...
typename remove_extent< T >::type remove_extent_t
Removes the first extent from the given array type.
Definition remove_extent.hpp:66
constexpr auto get(pair< T1, T2 > &values) noexcept -> tuple_element_t< I, pair< T1, T2 > > &
access an element of a pair
Definition pair.hpp:665
constexpr auto begin(Element(&array)[Size]) noexcept -> Element *
Obtain a pointer to the first element of the array.
Definition begin.hpp:28
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24
#define ARENE_NODISCARD
An alias for [[nodiscard]] , or a no-op if the attribute is not supported.
Definition nodiscard.hpp:26