Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
size.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::ranges::size"
2// Copyright 2026, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RANGES_SIZE_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RANGES_SIZE_HPP_
8
9// IWYU pragma: private, include "arene/base/ranges.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
12// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
19#include "arene/base/ranges/detail/is_sized_sentinel_for.hpp"
21#include "arene/base/stdlib_choice/declval.hpp"
22#include "arene/base/stdlib_choice/enable_if.hpp"
23#include "arene/base/stdlib_choice/forward.hpp"
24#include "arene/base/stdlib_choice/is_integral.hpp"
25#include "arene/base/stdlib_choice/make_unsigned.hpp"
26#include "arene/base/stdlib_choice/remove_cv.hpp"
27#include "arene/base/stdlib_choice/remove_reference.hpp"
30// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
31
32namespace arene {
33namespace base {
34namespace ranges {
35
36namespace size_detail {
37
38/// @brief Poison-pill declaration so that the unqualified call to @c size below performs argument-dependent lookup
39// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
40template <class>
41auto size() -> void = delete;
42// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
43
44/// @brief Alias to the result of @c t.size() , well-formed only when that member call is valid.
45/// @tparam Range the type of the range
46template <typename Range>
47using member_size_t = decltype(std::declval<Range>().size());
48
49/// @brief Alias to the result of the ADL call @c size(t) , well-formed only when that call is valid.
50/// @tparam Range the type of the range
51template <typename Range>
52using adl_size_t = decltype(size(std::declval<Range>()));
53
54/// @brief @c true when @c Range opts out of a constant-time @c size() via @c disable_sized_range .
55/// @tparam Range the type of the range
56template <typename Range>
57extern constexpr bool disabled_v = disable_sized_range_v<remove_cvref_t<Range>>;
58
59/// @brief @c true when @c T models the integer-like requirement
60/// @tparam T the candidate result type of a @c size() call
61///
62/// @note Implementing the integer-like requirement is extremely difficult in C++14, so the stricter @c std::is_integral
63/// trait is used instead.
64template <typename T>
65extern constexpr bool is_integer_like_v = std::is_integral<remove_cvref_t<T>>::value;
66
67/// @brief @c true when the member @c size() call is valid
68/// @tparam Range the type of the range
69template <typename Range>
70extern constexpr bool can_use_member_size_v = substitution_succeeds<member_size_t, Range>;
71
72/// @brief @c true when the ADL @c size() call is valid
73/// @tparam Range the type of the range
74template <typename Range>
75extern constexpr bool can_use_adl_size_v = substitution_succeeds<adl_size_t, Range>;
76
77// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
78/// @brief Function object implementing @c arene::base::ranges::size .
79class size_fn {
80 private:
81 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
82 /// @brief Returns the size of a range that provides a @c size() member function which returns an @c integer-like
83 /// @tparam Range the type of the range
84 /// @param range the range argument
85 /// @return a decay-copy of @c range.size()
86 template <
87 typename Range,
89 can_use_member_size_v<Range> && is_integer_like_v<member_size_t<Range>> && !disabled_v<Range>>> = nullptr>
90 static constexpr auto impl(Range&& range, priority_tag<2>) noexcept(
92 ) -> remove_cvref_t<member_size_t<Range>> {
93 return std::forward<Range>(range).size();
94 }
95
96 /// @brief Returns the size of a range that provides an ADL-findable @c size() free function which returns an @c
97 /// integer-like
98 /// @tparam Range the type of the range
99 /// @param range the range argument
100 /// @return a decay-copy of the ADL call @c size(range)
101 template <
102 typename Range,
104 can_use_adl_size_v<Range> && is_integer_like_v<adl_size_t<Range>> && !disabled_v<Range>>> = nullptr>
105 static constexpr auto impl(Range&& range, priority_tag<1>) noexcept(
107 ) -> remove_cvref_t<adl_size_t<Range>> {
108 return size(std::forward<Range>(range));
109 }
110
111 /// @brief Returns the size of a range computed as @c end(range) - begin(range) .
112 /// @tparam Range the type of the range
113 /// @param range the range argument
114 /// @return @c end(range) - begin(range) , cast to the unsigned version of the iterator's difference type
115 // Unlike @c std::ranges::size , no dedicated overload for C arrays is provided: this random-access
116 // <c> end - begin </c> path already gives the extent of a C array of known bound. A dedicated array overload could be
117 // added if a future need arises.
118 //
119 // This gate deliberately requires only @c is_sized_sentinel_for_v and omits the @c forward_iterator<I> requirement
120 // that @c std::ranges::size also imposes, so it accepts sized input-iterator ranges the standard would reject. There
121 // is no ITER_CONCEPT-based @c forward_iterator trait yet; one can be added and required here when broader
122 // @c std::ranges backporting or proxy-returning views make that distinction matter.
123 template <
124 typename Range,
126 static constexpr auto impl(Range&& range, priority_tag<0>) noexcept(
127 noexcept(arene::base::ranges::end(std::declval<Range>()) - arene::base::ranges::begin(std::declval<Range>()))
129 // parasoft-begin-suppress AUTOSAR-M5_0_15-a "False positive: first is an iterator, bounded by last"
131 arene::base::ranges::end(std::forward<Range>(range)) - arene::base::ranges::begin(std::forward<Range>(range))
132 );
133 // parasoft-end-suppress AUTOSAR-M5_0_15-a
134 // parasoft-end-suppress CERT_C-EXP37-a
135 }
136
137 public:
138 /// @brief Returns the size of a range
139 /// @tparam Range the type of the range
140 /// @param range the range argument
141 /// @return the size of the range
142 template <typename Range>
143 ARENE_NODISCARD constexpr auto operator()(Range&& range) const
144 noexcept(noexcept(impl(std::forward<Range>(range), priority_tag<2>{})))
145 -> decltype(impl(std::forward<Range>(range), priority_tag<2>{})) {
146 return impl(std::forward<Range>(range), priority_tag<2>{});
147 }
148};
149// parasoft-end-suppress AUTOSAR-M2_10_1-a
150
151} // namespace size_detail
152
153/// @brief Returns an integer equal to the size of a range.
154///
155/// @c size is intended to be a constant time operation. Callers of this function should generally assume this runs in
156/// constant time and is ill-formed for types which don't have a constant-time size, but we can not enforce this for
157/// range types that we don't control. Use @c arene::base::distance if you want to disclaim this constant time
158/// guarantee.
159///
160/// To container implementers: if your container has member or ADL size functions then @c size will
161/// pick them up and assume that they can operate in constant time. If your type's size is not constant time, specialize
162/// @c arene::base::ranges::disable_sized_range for it so that @c size ignores those functions.
163///
164// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
165// object used in multiple TUs."
166// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
167// object used in multiple TUs."
168ARENE_CPP14_INLINE_VARIABLE(size_detail::size_fn, size);
169// parasoft-end-suppress AUTOSAR-M7_3_3-a
170// parasoft-end-suppress CERT_CPP-DCL59-a
171
172} // namespace ranges
173} // namespace base
174} // namespace arene
175
176#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_RANGES_SIZE_HPP_
#define ARENE_CPP14_INLINE_VARIABLE(type, name)
Emulates C++17's inline variable declarations for function objects.
Definition cpp14_inline.hpp:69
Definition external_vector.hpp:803
typedef iterator_t
The type of the iterator for a given range.
Definition begin_end.hpp:186
constexpr bool disable_sized_range_v
Accessor for the value of disable_sized_range .
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
constexpr bool substitution_succeeds
Determines if a template can be instantiated successfully, similar to C++20's requires clause.
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
typename iter_difference_t_detail::iter_difference_t_impl< remove_cvref_t< Itr > >::type iter_difference_t
Computes the difference type of the given iterator type.
Definition iter_difference_t.hpp:87
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 make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252
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