Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
basic_insert_iterator.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_BASIC_INSERT_ITERATOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_BASIC_INSERT_ITERATOR_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <iterator>
12
13#include "stdlib/include/stdlib_detail/addressof.hpp"
14#include "stdlib/include/stdlib_detail/basic_iterator.hpp"
15#include "stdlib/include/stdlib_detail/declval.hpp"
16#include "stdlib/include/stdlib_detail/forward.hpp"
17#include "stdlib/include/stdlib_detail/is_move_constructible.hpp"
18#include "stdlib/include/stdlib_detail/is_same.hpp"
19#include "stdlib/include/stdlib_detail/iterator_tags.hpp"
20// parasoft-begin-suppress AUTOSAR-A16_2_2-a "False positive: This header *does* contain declarations necessary to
21// compile this file."
22#include "stdlib/include/stdlib_detail/move.hpp"
23// parasoft-end-suppress AUTOSAR-A16_2_2-a
24#include "stdlib/include/stdlib_detail/remove_cv.hpp"
25#include "stdlib/include/stdlib_detail/remove_reference.hpp"
26#include "stdlib/include/stdlib_detail/void_t.hpp"
27
28// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
29// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
30
31namespace std {
32
34
35/// @brief helper variable template to check if a type defines member @c value_type
36/// @tparam T type to check
37///
38/// This primary template is always @c false.
39///
40template <class T, class = void>
41extern constexpr bool has_value_type_member_impl{false};
42
43/// @brief helper variable template to check if a type defines member @c value_type
44/// @tparam T type to check
45///
46/// This template specialization is always @c true if @c T has member @c value_type.
47///
48template <class T>
49extern constexpr bool has_value_type_member_impl<T, std::void_t<typename T::value_type>>{true};
50
51/// @brief helper variable template to check if a type defines member @c value_type
52/// @tparam T type to check
53///
54/// This variable template invokes the variable template above in order to avoid
55/// displaying the second template parameter in compiler error messages.
56///
57template <class T>
59
60/// @brief helper variable template to check if a type defines member @c iterator
61/// @tparam T type to check
62///
63/// This primary template is always @c false.
64///
65template <class T, class = void>
66extern constexpr bool has_iterator_member_impl{false};
67
68/// @brief helper variable template to check if a type defines member @c iterator
69/// @tparam T type to check
70///
71/// This template specialization is always @c true if @c T has member @c iterator.
72///
73template <class T>
74extern constexpr bool has_iterator_member_impl<T, std::void_t<typename T::iterator>>{true};
75
76/// @brief helper variable template to check if a type defines member @c iterator
77/// @tparam T type to check
78///
79/// This variable template invokes the variable template above in order to avoid
80/// displaying the second template parameter in compiler error messages.
81///
82template <class T>
83extern constexpr bool has_iterator_member_v = has_iterator_member_impl<T>;
84
85// parasoft-begin-suppress AUTOSAR-M14_5_3-a "False positive: There is no template assignment operator with generic
86// parameter"
87
88/// @brief iterator adaptor for insertion into a container
89/// @tparam Container associated container type
90/// @tparam AdaptorMixin mixin specifying the container insert operation
91///
92/// The class template @c basic_insert_iterator is used to define the insert
93/// iterator adaptors:
94/// * @c back_insert_iterator
95/// * @c insert_iterator
96///
97/// @c AdaptorMixin specifies the container insert operation (e.g. @c push_back)
98/// and provides storage for the container pointer and other necessary members.
99///
100template <class Container, class AdaptorMixin>
102 : public iterator<output_iterator_tag, void, void, void, void>
103 , protected AdaptorMixin {
104 /// @brief determine if container insert operation is noexcept
105 /// @tparam T type to insert
106 ///
107 template <class T>
108 static constexpr bool is_nothrow_insertable_v{noexcept(std::declval<basic_insert_iterator&>().insert(std::declval<T>()
109 ))};
110
111 public:
112 /// @brief container type to insert into
113 ///
114 using container_type = Container;
115
116 /// @brief @c basic_insert_iterator constructor
117 ///
118 /// This type inherits constructors from the adaptor mixin.
119 ///
120 using AdaptorMixin::AdaptorMixin;
121
122 // parasoft-begin-suppress AUTOSAR-A12_8_6-a "False positive: these are
123 // neither copy assignment nor move assignment operators"
124
125 /// @brief inserts a value into the associated container
126 /// @param value value to insert
127 ///
128 /// Inserts @c value into the associated container with a call to the mixin's
129 /// @c insert member function.
130 ///
131 /// @returns @c *this
132 ///
133 constexpr auto operator=(typename Container::value_type const& value
134 ) noexcept(is_nothrow_insertable_v<typename Container::value_type const&>) -> basic_insert_iterator& {
135 this->insert(value);
136 return *this;
137 }
138
139 /// @brief inserts a value into the associated container
140 /// @param value value to insert
141 ///
142 /// Inserts @c std::move(value) into the associated container with a call to
143 /// the mixin's @c insert member function.
144 ///
145 /// @returns @c *this
146 ///
147 constexpr auto operator=(typename Container::value_type&& value
149 this->insert(std::move(value));
150 return *this;
151 }
152
153 // parasoft-end-suppress AUTOSAR-A12_8_6-a
154
155 /// @brief no-op
156 /// @returns @c *this
157 ///
158 constexpr auto operator*() noexcept -> basic_insert_iterator& { return *this; }
159
160 /// @brief no-op
161 /// @returns @c *this
162 ///
163 constexpr auto operator++() noexcept -> basic_insert_iterator& { return *this; }
164
165 // parasoft-begin-suppress AUTOSAR-A3_9_1-b "False positive: postincrement requires an int parameter"
166
167 /// @brief no-op
168 /// @returns @c *this
169 ///
170 constexpr auto operator++(int) noexcept -> basic_insert_iterator& { return *this; }
171
172 // parasoft-end-suppress AUTOSAR-A3_9_1-b
173};
174
175// parasoft-end-suppress AUTOSAR-M14_5_3-a
176
177} // namespace basic_insert_iterator_detail
178
180
181// parasoft-begin-suppress AUTOSAR-A14_5_1-a "False positive: There is no template constructor"
182
183/// @brief mixin used to define @c back_insert_iterator
184/// @tparam Container associated container type
185///
186template <class Container>
188 static_assert(
189 basic_insert_iterator_detail::has_value_type_member_v<Container>,
190 "'Container' does not satisfy syntactic requirements"
191 );
192
193 protected:
194 // Naming and access control for these members are specified by the standard.
195 //
196 // NOLINTBEGIN(misc-non-private-member-variables-in-classes,readability-identifier-naming)
197
198 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "Identifier 'container' must be
199 // declared 'protected' to be standard compliant"
200
201 /// @brief associated container to insert into
202 ///
203 Container* container;
204
205 // parasoft-end-suppress AUTOSAR-M11_0_1-a
206
207 // NOLINTEND(misc-non-private-member-variables-in-classes,readability-identifier-naming)
208
209 /// @brief insert an element into the associated container
210 /// @tparam T cv-ref qualified container value type
211 /// @param value value to insert
212 ///
213 template <class T>
214 constexpr auto insert(T&& value) noexcept(noexcept(container->push_back(std::forward<T>(value)))) -> void {
215 static_assert(
216 std::is_same_v<typename Container::value_type, std::remove_cv_t<std::remove_reference_t<T>>>,
217 "this internal function is only intended for use with cv-ref qualified container value types."
218 );
219 static_cast<void>(container->push_back(std::forward<T>(value)));
220 }
221
222 public:
223 /// @brief constructs an @c adaptor_mixin
224 /// @param cont container to insert into
225 ///
226 constexpr explicit adaptor_mixin(Container& cont) noexcept
228};
229
230// parasoft-end-suppress AUTOSAR-A14_5_1-a
231
232} // namespace back_insert_iterator_detail
233
234/// @brief iterator adaptor for insertion at the end of a container
235/// @tparam Container associated container type
236///
237/// @c std::back_insert_iterator is an OutputIterator that appends elements to a
238/// container for which it was constructed. The container's @c push_back()
239/// member function is called whenever the iterator (whether dereferenced or
240/// not) is assigned to. Incrementing the @c std::back_insert_iterator is a
241/// no-op.
242///
243template <class Container>
244using back_insert_iterator = basic_insert_iterator_detail::
246
247// parasoft-end-suppress AUTOSAR-M14_5_3-a
248// parasoft-end-suppress AUTOSAR-A14_5_1-a
249
250// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
251
252/// @brief creates a @c std::back_insert_iterator of type inferred from the
253/// argument
254/// @tparam Container container type to insert into
255/// @param container container to insert into
256///
257/// @c std::back_inserter is a convenience function template that constructs a
258/// @c std::back_insert_iterator for the container @c container with the type
259/// deduced from the type of the argument.
260///
261/// @return a @c std::back_insert_iterator which can be used to add elements to
262/// the end of the container @c container.
263///
264template <class Container>
265constexpr auto back_inserter(Container& container) noexcept -> back_insert_iterator<Container> {
266 return back_insert_iterator<Container>{container};
267}
268
269// parasoft-end-suppress AUTOSAR-M3_3_2-a
270
272
273/// @brief mixin used to define @c insert_iterator
274/// @tparam Container associated container type
275///
276template <class Container>
278 static_assert(
279 basic_insert_iterator_detail::has_value_type_member_v<Container>,
280 "'Container' does not satisfy syntactic requirements"
281 );
282
283 static_assert(
284 basic_insert_iterator_detail::has_iterator_member_v<Container>,
285 "'Container' does not satisfy syntactic requirements"
286 );
287
288 /// @brief base iterator type
289 ///
290 using iterator_type = typename Container::iterator;
291
292 protected:
293 // Naming and access control for these members are specified by the standard.
294 //
295 // NOLINTBEGIN(misc-non-private-member-variables-in-classes,readability-identifier-naming)
296
297 // parasoft-begin-suppress AUTOSAR-M11_0_1-a "Identifiers must be declared
298 // 'protected' to be standard compliant"
299
300 /// @brief associated container to insert into
301 ///
302 Container* container;
303
304 /// @brief location to insert into
305 ///
306 iterator_type iter;
307
308 // parasoft-end-suppress AUTOSAR-M11_0_1-a
309
310 // NOLINTEND(misc-non-private-member-variables-in-classes,readability-identifier-naming)
311
312 /// @brief insert an element into the associated container
313 /// @tparam T cv-ref qualified container value type
314 /// @param value value to insert
315 ///
316 template <class T>
317 constexpr auto insert(T&& value) noexcept(noexcept(
320 )) -> void {
321 static_assert(
322 std::is_same_v<typename Container::value_type, std::remove_cv_t<std::remove_reference_t<T>>>,
323 "this internal function is only intended for use with cv-ref qualified container value types."
324 );
325 iter = container->insert(iter, std::forward<T>(value));
326 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "This is an iterator type, so incrementing is OK"
327 static_cast<void>(++iter);
328 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
329 }
330
331 public:
332 /// @brief constructs an @c adaptor_mixin
333 /// @param cont container to insert into
334 /// @param pos iterator in @c cont specifying the insertion position
335 ///
336 constexpr adaptor_mixin(Container& cont, typename Container::iterator pos) noexcept
338 iter{std::move(pos)} {}
339};
340
341} // namespace insert_iterator_detail
342
343/// @brief iterator adaptor for insertion into a container
344/// @tparam Container associated container type
345///
346/// @c std::insert_iterator is an OutputIterator that inserts elements into a
347/// container for which it was constructed, at the position pointed to by the
348/// supplied iterator. The container's @c insert() member function is called
349/// whenever the iterator (whether dereferenced or not) is assigned to.
350/// Incrementing the @c std::insert_iterator is a no-op.
351///
352/// @note Inserting multiple elements with @c std::insert_iterator places the
353/// earliest elements first.
354///
355template <class Container>
356using insert_iterator =
358
359// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
360
361/// @brief creates an @c std::insert_iterator of type inferred from the
362/// argument
363/// @tparam Container container type to insert into
364/// @param container container to insert into
365/// @param iter iterator in @c container specifying the insertion position
366///
367/// @c std::inserter is a convenience function template that constructs an @c
368/// std::insert_iterator for the container @c container and its iterator @c iter
369/// with the type deduced from the type of the @c container argument.
370///
371/// @return a @c std::insert_iterator which can be used to insert elements into
372/// the container @c container at the position specified by @c iter.
373///
374template <class Container>
375constexpr auto inserter(
376 Container& container,
377 typename Container::iterator iter
379 return {container, std::move(iter)};
380}
381
382// parasoft-end-suppress AUTOSAR-M3_3_2-a
383
384} // namespace std
385
386#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_BASIC_INSERT_ITERATOR_HPP_
mixin used to define back_insert_iterator
Definition basic_insert_iterator.hpp:187
Container * container
associated container to insert into
Definition basic_insert_iterator.hpp:203
constexpr auto insert(T &&value) noexcept(noexcept(container->push_back(std::forward< T >(value)))) -> void
insert an element into the associated container
Definition basic_insert_iterator.hpp:214
constexpr adaptor_mixin(Container &cont) noexcept
constructs an adaptor_mixin
Definition basic_insert_iterator.hpp:226
iterator adaptor for insertion into a container
Definition basic_insert_iterator.hpp:103
constexpr auto operator=(typename Container::value_type &&value) noexcept(is_nothrow_insertable_v< typename Container::value_type && >) -> basic_insert_iterator &
inserts a value into the associated container
Definition basic_insert_iterator.hpp:147
constexpr auto operator++() noexcept -> basic_insert_iterator &
no-op
Definition basic_insert_iterator.hpp:163
constexpr auto operator++(int) noexcept -> basic_insert_iterator &
no-op
Definition basic_insert_iterator.hpp:170
constexpr auto operator=(typename Container::value_type const &value) noexcept(is_nothrow_insertable_v< typename Container::value_type const & >) -> basic_insert_iterator &
inserts a value into the associated container
Definition basic_insert_iterator.hpp:133
constexpr auto operator*() noexcept -> basic_insert_iterator &
no-op
Definition basic_insert_iterator.hpp:158
mixin used to define insert_iterator
Definition basic_insert_iterator.hpp:277
constexpr adaptor_mixin(Container &cont, typename Container::iterator pos) noexcept
constructs an adaptor_mixin
Definition basic_insert_iterator.hpp:336
Container * container
associated container to insert into
Definition basic_insert_iterator.hpp:302
constexpr auto insert(T &&value) noexcept(noexcept(std::declval< iterator_type & >()=container->insert(std::declval< iterator_type & >(), std::forward< T >(value)),++std::declval< iterator_type & >())) -> void
insert an element into the associated container
Definition basic_insert_iterator.hpp:317
iterator_type iter
location to insert into
Definition basic_insert_iterator.hpp:306
Definition basic_insert_iterator.hpp:179
Definition basic_insert_iterator.hpp:33
constexpr bool has_value_type_member_v
helper variable template to check if a type defines member value_type
constexpr bool has_iterator_member_v
helper variable template to check if a type defines member iterator
constexpr bool has_value_type_member_impl
helper variable template to check if a type defines member value_type
constexpr bool has_iterator_member_impl
helper variable template to check if a type defines member iterator
Definition basic_insert_iterator.hpp:271
constexpr auto back_inserter(Container &container) noexcept -> back_insert_iterator< Container >
creates a std::back_insert_iterator of type inferred from the argument
Definition basic_insert_iterator.hpp:265
constexpr auto inserter(Container &container, typename Container::iterator iter) noexcept(std::is_nothrow_move_constructible_v< typename Container::iterator >) -> insert_iterator< Container >
creates an std::insert_iterator of type inferred from the argument
Definition basic_insert_iterator.hpp:375
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827