Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
iter_move.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_move"
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_MOVE_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ITER_MOVE_HPP_
8
9// IWYU pragma: private, include "arene/base/iterator.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
15#include "arene/base/iterator/detail/is_dereferenceable.hpp"
16#include "arene/base/stdlib_choice/declval.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/forward.hpp"
19#include "arene/base/stdlib_choice/is_class.hpp"
20#include "arene/base/stdlib_choice/is_enum.hpp"
21#include "arene/base/stdlib_choice/is_lvalue_reference.hpp"
22#include "arene/base/stdlib_choice/move.hpp"
23#include "arene/base/stdlib_choice/remove_reference.hpp"
24
25namespace arene {
26namespace base {
27namespace ranges {
28
29namespace itr_move_detail {
30
31/// @brief Deleted fallback for iter_move to ensure adl only finds function overloads
32template <typename>
33auto iter_move() -> void = delete;
34
35/// @brief Implementation of @c arene::base::ranges::iter_move
36/// @see arene::base::ranges::iter_move
37class iter_move_fn {
38 /// @brief Result type of the ADL-based @c iter_move for a given iterator type.
39 /// @tparam Itr The type of the iterator for which to determine the result type.
40 template <typename Itr>
41 using adl_itr_move_rt = decltype(iter_move(std::declval<Itr&&>()));
42 /// @brief Helper variable to determine if ADL-based @c iter_move is available for a given iterator type.
43 /// @tparam Itr The type of the iterator to check for ADL-based @c iter_move availability.
44 template <typename Itr>
45 static constexpr bool has_adl_iter_move_v = substitution_succeeds<adl_itr_move_rt, Itr>;
46 /// @brief Helper type to determine the type obtained by dereferencing an iterator.
47 /// @tparam Itr The type of the iterator for which to determine the dereferenced type.
48 template <typename Itr>
49 using deref_t = decltype(*std::declval<Itr>());
50
51 public:
52 // parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
53 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifier 'iterator' does not hide anything"
54 /// @brief Specialization for when ADL-based @c iter_move is available for the given iterator type.
55 /// @tparam Itr The type of the iterator for which to determine the result type.
56 /// @param iterator The iterator to apply ADL-based @c iter_move to.
57 /// @return The result of applying ADL-based @c iter_move to the iterator.
58 template <
59 typename Itr,
62 std::is_class<std::remove_reference_t<Itr>>::value || std::is_enum<std::remove_reference_t<Itr>>::value>,
64 constexpr auto operator()(Itr&& iterator) const noexcept(noexcept(iter_move(std::declval<Itr>())))
65 -> adl_itr_move_rt<Itr> {
66 return iter_move(std::forward<Itr>(iterator));
67 }
68
69 // parasoft-begin-suppress AUTOSAR-A18_9_2-a "Use of 'forward' here ensures rvalues are correctly moved"
70 // parasoft-begin-suppress AUTOSAR-A18_9_3-a-2 "False Positive: Generic code, moved object is not explicitly const"
71 /// @brief Specialization for when dereferenced type is an lvalue reference.
72 /// @tparam Itr The type of the iterator for which to determine the result type.
73 /// @param iterator The iterator to apply the default implementation of @c iter_move to.
74 /// @return @c std::move(*itr)
75 template <
76 typename Itr,
77 typename DerefT = deref_t<Itr&&>,
81 std::enable_if_t<std::is_lvalue_reference<DerefT>::value>> = nullptr>
82 constexpr auto operator()(Itr&& iterator) const noexcept(noexcept(std::move(*std::declval<Itr>())))
83 -> decltype(std::move(*std::forward<Itr>(iterator))) {
84 return std::move(*std::forward<Itr>(iterator));
85 }
86 // parasoft-end-suppress AUTOSAR-A18_9_2-a "Use of 'forward' here ensures rvalues are correctly moved"
87 // parasoft-end-suppress AUTOSAR-A18_9_3-a-2 "False Positive: Generic code, moved object is not explicitly const"
88
89 /// @brief Specialization for when dereferenced type is not an lvalue reference.
90 /// @tparam Itr The type of the iterator for which to determine the result type.
91 /// @param iterator The iterator to apply the default implementation of @c iter_move to.
92 /// @return @c *itr
93 template <
94 typename Itr,
95 typename DerefT = deref_t<Itr&&>,
99 std::enable_if_t<!std::is_lvalue_reference<DerefT>::value>> = nullptr>
100 constexpr auto operator()(Itr&& iterator) const noexcept(noexcept(*std::declval<Itr>()))
101 -> decltype(*std::forward<Itr>(iterator)) {
102 return *std::forward<Itr>(iterator);
103 }
104};
105
106} // namespace itr_move_detail
107
108/// @brief Performs the move operation on dereferenced value of a dereferenceable type.
109/// @tparam Itr The type of the iterator to apply iter_move to.
110/// The iterator to move from is passed as @c iterator.
111/// @return The result of applying iter_move to the iterator.
112///
113/// This operation is expression-equivalent to one of the following operations, in order of preference:
114/// 1. If @c Itr satisfies either @c std::is_class or @c std::is_enum , and @c iter_move(itr) is well-formed by argument
115/// dependent lookup, returns @c iter_move(itr) .
116/// 2. Otherwise, if @c *itr is well-formed:
117/// - if @c *itr is an lvalue reference, returns @c std::move(*itr) .
118/// - otherwise, returns @c *itr .
119///
120/// If none of the requirements are satisfied, then @c iter_move is ill-formed. This can cause a substitution failure
121/// when @c iter_move appears in the context of a template instantiation.
122// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
123// object used in multiple TUs."
124// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
125// object used in multiple TUs."
126// parasoft-begin-suppress CERT_CPP-DCL56-a "False positive: the referenced constexpr variable is initialized"
127ARENE_CPP14_INLINE_VARIABLE(itr_move_detail::iter_move_fn, iter_move);
128// parasoft-end-suppress CERT_CPP-DCL56-a
129// parasoft-end-suppress AUTOSAR-M7_3_3-a
130// parasoft-end-suppress CERT_CPP-DCL59-a
131
132} // namespace ranges
133} // namespace base
134} // namespace arene
135
136#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_ITER_MOVE_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
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
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
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,...
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