Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
incrementable_traits.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_ARENE_BASE_ITERATOR_INCREMENTABLE_TRAITS_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_INCREMENTABLE_TRAITS_HPP_
7
8// IWYU pragma: private, include "arene/base/iterator.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/stdlib_choice/cstddef.hpp"
15#include "arene/base/stdlib_choice/declval.hpp"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/is_integral.hpp"
18#include "arene/base/stdlib_choice/is_object.hpp"
19#include "arene/base/stdlib_choice/make_signed.hpp"
20// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
21
22namespace arene {
23namespace base {
24
25namespace incrementable_traits_detail {
26
27/// @brief Helper for deducing the difference type of an iterator type
28/// @tparam Itr The iterator type
29/// @see ::arene::base::incrementable_traits<Itr>
30template <typename Itr, typename = constraints<>>
31class incrementable_traits_impl {};
32
33/// @brief Deduces the difference type of an iterator type from its member typedef @c difference_type
34/// @tparam Itr The iterator type
35template <typename Itr>
36using use_difference_type = typename Itr::difference_type;
37
38/// @brief Helper to determine if an iterator type has a member typedef @c difference_type
39/// @tparam Itr The iterator type to check
40template <typename Itr>
41constexpr bool has_difference_type_v = substitution_succeeds<use_difference_type, Itr>;
42
43/// @brief Specialization for types that have a member typedef @c difference_type
44/// @tparam Itr The iterator type. Must satisfy @c has_difference_type_v<Itr>
45template <typename Itr>
46class incrementable_traits_impl<Itr, constraints<std::enable_if_t<has_difference_type_v<Itr>>>> {
47 public:
48 /// @brief The difference type of the iterator type.
49 using difference_type = typename Itr::difference_type;
50};
51
52/// @brief Deduces the difference type of an iterator type from the result of its subtraction operator.
53/// @tparam Itr The iterator type
54template <typename Itr>
55using subtraction_result_t = decltype(std::declval<Itr const&>() - std::declval<Itr const&>());
56
57/// @brief Helper to determine if an iterator type has a subtraction operator
58/// @tparam Itr The iterator type to check
59template <typename Itr>
60constexpr bool has_subtraction_result_v = substitution_succeeds<subtraction_result_t, Itr>;
61
62/// @brief Helper to determine if an iterator type has a subtraction operator that returns an integral type
63/// @tparam Itr The iterator type to check
64template <typename Itr, bool = has_subtraction_result_v<Itr>>
65constexpr bool has_integral_subtraction_result_v = false;
66
67/// @brief Helper to determine if an iterator type has a subtraction operator that returns an integral type
68/// @tparam Itr The iterator type to check
69template <typename Itr>
70constexpr bool has_integral_subtraction_result_v<Itr, true> = std::is_integral<subtraction_result_t<Itr>>::value;
71
72/// @brief Specialization for when the iterator doesn't have iterator_traits but does have subtraction operator
73/// @tparam Itr The iterator type. Must satisfy @c has_integral_subtraction_result_v<Itr>
74template <typename Itr>
75class incrementable_traits_impl<
76 Itr,
78 std::enable_if_t<!has_difference_type_v<Itr>>,
79 std::enable_if_t<has_integral_subtraction_result_v<Itr>>>> {
80 public:
81 /// @brief The difference type of the iterator type.
82 using difference_type = std::make_signed_t<subtraction_result_t<Itr>>;
83};
84
85/// @brief Specialization of @c incrementable_traits for pointer types.
86///
87/// @tparam T the type pointed to by the pointer type.
88template <typename T, bool = std::is_object<T>::value>
89class incrementable_traits_pointer_specialization {
90 public:
91 /// @brief The difference type of the pointer type.
92 using difference_type = std::ptrdiff_t;
93};
94
95/// @brief Specialization of @c incrementable_traits for pointer types where @c T is not an object type.
96template <typename T>
97class incrementable_traits_pointer_specialization<T, false> {};
98
99} // namespace incrementable_traits_detail
100
101/// @brief Computes the difference type of an incrementable iterator type.
102///
103/// @tparam Itr The incrementable iterator type for which to compute the difference type.
104///
105/// The member typedef @c difference_type will be present and defined if either of the following are true, in order of
106/// preference:
107/// 1. A specialization of @c incrementable_traits is provided for @c Itr that defines a member typedef
108/// @c difference_type.
109/// 2. @c Itr has a member typedef @c difference_type.
110/// 3. The expression @c std::declval<Itr>()-std::declval<Itr>() is well-formed and has an integral type.
111///
112/// Otherwise, the member typedef @c difference_type will not be present.
113template <typename Itr>
114class incrementable_traits : public incrementable_traits_detail::incrementable_traits_impl<Itr> {};
115
116/// @brief Specialization of @c incrementable_traits for pointer types.
117/// @tparam T the type pointed to by the pointer type.
118///
119/// If @c T satisfies @c std::is_object<T>, the member typedef @c difference_type will be defined as @c std::ptrdiff_t ,
120/// otherwise it will be absent.
121template <typename T>
122class incrementable_traits<T*> : public incrementable_traits_detail::incrementable_traits_pointer_specialization<T> {};
123
124/// @brief Specialization of @c incrementable_traits for const types, dispatches to the non-const specialization.
125/// @tparam Itr the const type for which to compute the difference type.
126/// @see ::arene::base::incrementable_traits<Itr>
127template <typename Itr>
128class incrementable_traits<Itr const> : public incrementable_traits<Itr> {};
129
130} // namespace base
131} // namespace arene
132
133#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_INCREMENTABLE_TRAITS_HPP_
Computes the difference type of an incrementable iterator type.
Definition incrementable_traits.hpp:114
Definition accumulate.hpp:37
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
Definition accumulate.hpp:36
typename make_signed< Type >::type make_signed_t
A type alias for the equivalent signed type for integral and enumeration types other than bool.
Definition make_signed.hpp:252
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
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,...