Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
accumulate.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_ACCUMULATE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ACCUMULATE_HPP_
7
8// IWYU pragma: private, include <numeric>
9// IWYU pragma: friend "stdlib_detail/.*"
10
11// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
12// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
13
14#include "arene/base/constraints.hpp"
15#include "arene/base/utility/make_subrange.hpp"
16#include "stdlib/include/stdlib_detail/enable_if.hpp"
17#include "stdlib/include/stdlib_detail/forward.hpp"
18#include "stdlib/include/stdlib_detail/is_assignable.hpp"
19#include "stdlib/include/stdlib_detail/is_copy_assignable.hpp"
20#include "stdlib/include/stdlib_detail/is_copy_constructible.hpp"
21#include "stdlib/include/stdlib_detail/iterator_concepts.hpp"
22
23namespace std {
24
25// parasoft-begin-suppress CERT_CPP-ERR55-a-2 "False positive: Exception specification is conditional"
26// parasoft-begin-suppress CERT_CPP-ERR50-h-3 "False positive: Exception specification is conditional"
27// parasoft-begin-suppress AUTOSAR-A15_5_3-h-2 "False positive: Exception specification is conditional"
28// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
29/// @brief Perform a left-fold on the input range @c [first,last) starting with the value @c init . For each element @c
30/// e in the range, does @c init=init+e
31/// @tparam InputIterator The iterator type of the range
32/// @tparam T The type of the initial value and return value
33/// @param first The start of the range
34/// @param last The end of the range
35/// @param init The initial value
36/// @return @c init as modified by the operations
37/// @pre @c InputIterator must be a valid input iterator
38/// @pre @c [first,last) must be a valid range
39/// @pre @c init=init+*first must be defined.
40/// @throws Any exception thrown from iterator operations, or @c init=init+e
41template <
42 typename InputIterator,
43 typename T,
47 std::declval<T&>() = std::declval<T&>() + *std::declval<InputIterator&>()
48 )
49) -> T {
50 static_assert(
52 "The 'init' value must be a copy-constructible and copy-assignable type"
53 );
54 // parasoft-begin-suppress AUTOSAR-A7_1_5-a "False Positive: 'element' has return type of range element, which may
55 // be non-fundamental"
56 for (auto&& element : arene::base::make_subrange(first, last)) {
57 // parasoft-end-suppress AUTOSAR-A7_1_5-a
58 // parasoft-begin-suppress CERT_CPP-ERR51-b "False positive: Thrown exception is propagated"
59 // parasoft-begin-suppress AUTOSAR-A15_3_2-a "False positive: Thrown exception is propagated"
60 // parasoft-begin-suppress AUTOSAR-A15_5_3-g "False positive: Thrown exception is propagated"
61 // parasoft-begin-suppress CERT_CPP-ERR50-g "False positive: Thrown exception is propagated"
62 // parasoft-begin-suppress AUTOSAR-M15_3_4-b "False positive: Thrown exception is propagated"
63 // parasoft-begin-suppress AUTOSAR-A15_2_1 "False positive: Thrown exception is propagated"
64 init = init + std::forward<decltype(element)>(element);
65 // parasoft-end-suppress AUTOSAR-A15_2_1
66 // parasoft-end-suppress AUTOSAR-M15_3_4-b
67 // parasoft-end-suppress CERT_CPP-ERR50-g
68 // parasoft-end-suppress AUTOSAR-A15_5_3-g
69 // parasoft-end-suppress AUTOSAR-A15_3_2-a
70 // parasoft-end-suppress CERT_CPP-ERR51-b
71 }
72
73 return init;
74}
75// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
76// parasoft-end-suppress CERT_CPP-ERR51-b-3
77// parasoft-end-suppress AUTOSAR-A15_5_3-h-2
78// parasoft-end-suppress CERT_CPP-ERR55-a-2
79// parasoft-end-suppress CERT_CPP-ERR50-h-3
80
81// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
82/// @brief Perform a left-fold on the input range @c [first,last) starting with the value @c init using the specified @c
83/// binary_op . For each element @c e in the range, does @c init=binary_op(init,e)
84/// @tparam InputIterator The iterator type of the range
85/// @tparam T The type of the initial value and return value
86/// @tparam BinaryOperation The type of the binary operation
87/// @param first The start of the range
88/// @param last The end of the range
89/// @param init The initial value
90/// @param binary_op The operator to use to accumulate the values
91/// @return @c init as modified by the operations
92/// @pre @c InputIterator must be a valid input iterator
93/// @pre @c [first,last) must be a valid range
94/// @pre @c init=binary_op(init,*first) must be defined.
95/// @throws Any exception thrown from iterator operations, or @c init=binary_op(init,*first)
96template <
97 typename InputIterator,
98 typename T,
99 typename BinaryOperation,
103 T&,
104 decltype(std::declval<BinaryOperation&>()(std::declval<T&>(), *std::declval<InputIterator&>()))>>> =
105 nullptr>
109 )
110) -> T {
111 static_assert(
113 "The 'init' value must be a copy-constructible and copy-assignable type"
114 );
115 // parasoft-begin-suppress AUTOSAR-A7_1_5-a "False Positive: 'element' has return type of range element, which may
116 // be non-fundamental"
117 for (auto&& element : arene::base::make_subrange(first, last)) {
118 // parasoft-end-suppress AUTOSAR-A7_1_5-a
119 init = binary_op(init, std::forward<decltype(element)>(element));
120 }
121 return init;
122}
123// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2
124
125} // namespace std
126
127#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_ACCUMULATE_HPP_
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