Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
iter_difference_t.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::iter_difference_t"
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_ITERATOR_ITER_DIFFERENCE_T_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ITER_DIFFERENCE_T_HPP_
8
9// IWYU pragma: private, include "arene/base/iterator.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"
16#include "arene/base/stdlib_choice/enable_if.hpp"
17#include "arene/base/stdlib_choice/iterator_traits.hpp"
18#include "arene/base/stdlib_choice/remove_cv.hpp"
19#include "arene/base/stdlib_choice/remove_reference.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
22
23namespace arene {
24namespace base {
25
26namespace iter_difference_t_detail {
27
28/// @brief Helper variable template to determine if @c std::iterator_traits<Itr>::difference_type exists.
29/// @tparam Itr The iterator type to check.
30template <typename Itr>
31using use_iterator_traits_difference_type = typename std::iterator_traits<Itr>::difference_type;
32
33/// @brief Helper variable template to determine if @c std::iterator_traits<Itr>::difference_type exists.
34/// @tparam Itr The iterator type to check.
35template <typename Itr>
36constexpr bool has_iterator_traits_difference_type_v = substitution_succeeds<use_iterator_traits_difference_type, Itr>;
37
38/// @brief Helper alias for checking if @c incrementable_traits<Itr>::difference_type exists.
39/// @tparam Itr The iterator type to check.
40template <typename Itr>
41using use_incrementable_traits_difference_type = typename incrementable_traits<Itr>::difference_type;
42
43/// @brief Helper variable to determine if @c incrementable_traits<Itr>::difference_type exists.
44/// @tparam Itr The iterator type to check.
45template <typename Itr>
46constexpr bool has_incrementable_traits_difference_type_v =
48
49/// @brief Implementation detail for computing the difference type of an iterator.
50/// @tparam Itr The iterator type for which to compute the difference type.
51template <typename Itr, typename = constraints<>>
52class iter_difference_t_impl {};
53
54/// @brief Implementation detail for computing the difference type of an iterator.
55/// @tparam Itr The iterator type for which to compute the difference type.
56/// Specialization for when @c std::iterator_traits<Itr>::difference_type exists.
57template <typename Itr>
58class iter_difference_t_impl<Itr, constraints<std::enable_if_t<has_iterator_traits_difference_type_v<Itr>>>> {
59 public:
60 /// @brief The difference type of the iterator type.
61 using type = typename std::iterator_traits<Itr>::difference_type;
62};
63
64/// @brief Implementation detail for computing the difference type of an iterator.
65/// @tparam Itr The iterator type for which to compute the difference type.
66/// Specialization for when @c std::iterator_traits<Itr>::difference_type does not exist but
67/// @c incrementable_traits<Itr>::difference_type does.
68template <typename Itr>
69class iter_difference_t_impl<
70 Itr,
72 std::enable_if_t<!has_iterator_traits_difference_type_v<Itr>>,
73 std::enable_if_t<has_incrementable_traits_difference_type_v<Itr>>>> {
74 public:
75 /// @brief The difference type of the iterator type.
76 using type = typename incrementable_traits<Itr>::difference_type;
77};
78
79} // namespace iter_difference_t_detail
80
81/// @brief Computes the difference type of the given iterator type.
82/// @tparam Itr The iterator type for which to compute the difference type.
83/// The difference type is defined as follows:
84/// - If @c std::iterator_traits<remove_cvref_t<Itr>>::difference_type exists, it is used.
85/// - Otherwise, @c incrementable_traits<remove_cvref_t<Itr>>::difference_type is used.
86template <typename Itr>
87using iter_difference_t = typename iter_difference_t_detail::iter_difference_t_impl<remove_cvref_t<Itr>>::type;
88
89} // namespace base
90} // namespace arene
91
92#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ITER_DIFFERENCE_T_HPP_
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
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