Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
reference_wrapper.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_REFERENCE_WRAPPER_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_REFERENCE_WRAPPER_HPP_
7
8// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
9// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
10
11// IWYU pragma: private, include <functional>
12// IWYU pragma: friend "stdlib_detail/.*"
13
30
31namespace std {
32
34
35/// @brief Add argument member types to reference wrapped functions and member
36/// function pointers.
37/// The types added depend on the number of arguments expected by the wrapped type.
38/// For member function pointers the class pointer is considered as the first argument.
39///
40/// Default case, do not add any argument member types
41///
42/// @tparam Args List of args to the wrapped function
43template <typename TypeList>
45
46/// @brief One argument case, will add @c argument_type
47/// @tparam Single argument to the wrapped function
48template <typename Arg>
49class mixin_argument_types<arene::base::type_list<Arg>> {
50 public:
51 /// @brief Type of the wrapped function's argument
52 using argument_type = Arg;
53};
54
55/// @brief Two argument case, will add @c first_argument_type and @c second_argument_type
56/// @tparam Arg1 First argument to the wrapped function
57/// @tparam Arg2 Second argument to the wrapped function
58template <typename Arg1, typename Arg2>
59class mixin_argument_types<arene::base::type_list<Arg1, Arg2>> {
60 public:
61 /// @brief Type of the wrapped function's first argument
62 using first_argument_type = Arg1;
63 /// @brief Type of the wrapped function's second argument
65};
66
67/// @brief Forward member type @c result_type from a reference wrapped class
68/// containing that member type, if present
69///
70/// Case where @c reference_type is not present
71///
72/// @tparam T Wrapped class to forward type from
73template <typename T, typename = arene::base::constraints<>>
75
76/// @brief Case where @c reference_type is present and forwarded
77/// @tparam T Wrapped class to forward type from
78template <typename T>
79class mixin_class_result_type<T, arene::base::constraints<typename T::result_type>> {
80 public:
81 /// @brief Forwarded type from the wrapped class
82 using result_type = typename T::result_type;
83};
84
85/// @brief Forward member type @c argument_type from a reference wrapped class
86/// containing that member type, if present
87///
88/// Case where @c argument_type is not present
89///
90/// @tparam T Wrapped class to forward types from
91template <typename T, typename = arene::base::constraints<>>
93
94/// @brief Case where @c argument_type is present and forwarded
95/// @tparam T Wrapped class to forward type from
96template <typename T>
97class mixin_class_unary_argument_type<T, arene::base::constraints<typename T::argument_type>> {
98 public:
99 /// @brief Forwarded type from the wrapped class
100 using argument_type = typename T::argument_type;
101};
102
103/// @brief Forward member types @c first_argument_type and @c second_argument_type
104/// from a reference wrapped class containing those member types, if present
105///
106/// Case where @c first_argument_type and @c second_argument_type are not present
107///
108/// @tparam T Wrapped class to forward types from
109template <typename T, typename = arene::base::constraints<>>
110/// @brief Forwarded type from the wrapped class
112
113/// @brief Case where @c first_argument_type and @c second_argument_type are
114/// present and forwarded
115/// @tparam T Wrapped class to forward types from
116template <typename T>
118 T,
119 arene::base::constraints<typename T::first_argument_type, typename T::second_argument_type>> {
120 public:
121 /// @brief Forwarded type from the wrapped class
122 using first_argument_type = typename T::first_argument_type;
123 /// @brief Forwarded type from the wrapped class
124 using second_argument_type = typename T::second_argument_type;
125};
126
127/// @brief Add member types to @c reference_wrapper based on the underlying wrapped type
128///
129/// Base case where the wrapped type does not match requirements and no member
130/// types are added
131///
132/// @tparam T Wrapped type to check
133template <typename T, typename = arene::base::constraints<>>
135
136/// @brief Wrapped function case, sets @c result_type based on the return type
137/// of the function and potentially types based on argument types
138/// @tparam T Function to extract types from
139template <typename T>
140class mixin_function_types<T, arene::base::constraints<std::enable_if_t<std::is_function_v<std::remove_pointer_t<T>>>>>
141 : public mixin_argument_types<arene::base::function_arguments_t<std::remove_pointer_t<T>>> {
142 public:
143 /// @brief Return type of the wrapped function
145};
146
147/// @brief Wrapped member function pointer case, sets @c result_type based on
148/// the return type of the function and potentially types based on member class
149/// and argument types
150/// @tparam T Member function pointer to extract types from
151template <typename T>
152class mixin_function_types<T, arene::base::constraints<std::enable_if_t<std::is_member_function_pointer_v<T>>>>
153 : public mixin_argument_types<arene::base::type_lists::concat_t<
154 arene::base::type_list<std::remove_reference_t<
155 typename arene::base::member_function_pointer_traits<T>::implicit_argument_type>*>,
156 arene::base::member_function_arguments_t<T>>> {
157 public:
158 /// @brief Return type of the wrapped member function pointer
160};
161
162/// @brief Wrapped class case, potentially forwards member types if present
163/// within the class
164/// @tparam T Class type to forward types from
165template <typename T>
166class mixin_function_types<T, arene::base::constraints<std::enable_if_t<std::is_class_v<T>>>>
167 : public mixin_class_result_type<T>
170
171} // namespace reference_wrapper_detail
172
173// parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: 'reference_wrapper' does not hide an identifier in
174// 'tuple'"
175/// @brief wraps a reference in a copyable and assignable object
176/// @tparam T Type of the reference
177template <typename T>
179 // parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: there is only 1 level of pointer indirection"
180 /// @brief The wrapped reference
181 T* pointer_;
182 // parasoft-end-suppress AUTOSAR-A5_0_3-a
183
184 public:
185 /// @brief Wrapped type
186 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: this is in a different scope from other 'type's"
187 using type = T;
188 // parasoft-end-suppress AUTOSAR-A2_10_1-e
189
190 // TODO: Implement member types for functions
191
192 /// @brief Construct a reference_wrapper around a given input. Wrapped object must
193 /// have an lvalue reference address and not be the same type of
194 /// @c reference_wrapper.
195 /// @tparam U Type of passed object or function to wrap
196 /// @param to_wrap or function to wrap
197 // NOLINTNEXTLINE(hicpp-explicit-conversions) Not explicit per C++14 spec
198 reference_wrapper(T& to_wrap) noexcept
199 : pointer_{addressof(to_wrap)} {}
200
201 // parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
202 /// @brief Cannot be constructed from an rvalue reference
203 reference_wrapper(T&&) = delete;
204 // parasoft-end-suppress CERT_C-EXP37-a
205
206 /// @brief Access the stored reference
207 /// @return The reference wrapped by this instance
208 auto get() const noexcept -> T& { return *pointer_; }
209
210 // parasoft-begin-suppress AUTOSAR-A13_5_2-a "Not explicit per C++14 spec"
211 // parasoft-begin-suppress AUTOSAR-A13_2_3-a "False positive: Not a relational operator"
212 /// @brief Access the stored reference
213 /// @return The reference wrapped by this instance
214 // NOLINTNEXTLINE(hicpp-explicit-conversions) Not explicit per C++14 spec
215 operator T&() const noexcept { return get(); }
216 // parasoft-end-suppress AUTOSAR-A13_5_2-a
217 // parasoft-end-suppress AUTOSAR-A13_2_3-a
218
219 // parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar identifiers permitted by M2-10-1 Permit #1 v1.0.0"
220 /// @brief Calls the stored function with given args. Only available if the stored
221 /// type is callable.
222 /// @param args Arguments to forward to the stored function
223 /// @return The result of invoking the wrapped function
224 template <class... Args, arene::base::constraints<enable_if_t<arene::base::is_invocable_v<T&, Args...>>> = nullptr>
225 auto operator()(Args&&... args) const noexcept(arene::base::is_nothrow_invocable_v<T&, Args...>)
227 return arene::base::invoke(get(), std::forward<Args>(args)...);
228 }
229 // parasoft-end-suppress AUTOSAR-M2_10_1-a
230};
231// parasoft-end-suppress AUTOSAR-A2_10_1-e
232
233// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
234/// @brief Create a type-deduced @c reference_wrapper
235/// @tparam T type of the reference
236/// @param to_wrap Object or function to wrap
237/// @return A @c reference_wrapper around the input
238template <typename T>
239auto ref(T& to_wrap) noexcept -> reference_wrapper<T> {
240 return reference_wrapper<T>{to_wrap};
241}
242// parasoft-end-suppress AUTOSAR-M3_3_2-a
243
244/// @brief Create a type-deduced copy of a @c reference_wrapper
245/// @tparam T Type wrapped by the input wrapper
246/// @param other @c reference_wrapper to copy
247/// @return A copy of the input @c reference_wrapper
248template <typename T>
250 return other;
251}
252
253// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
254/// @brief Cannot create a @c reference_wrapper from an rvalue reference
255/// @tparam T type of the reference
256template <typename T>
257auto ref(T&&) = delete;
258// parasoft-end-suppress CERT_C-EXP37-a
259
260// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
261/// @brief Create a const type-deduced @c reference_wrapper
262/// @tparam T type of the reference
263/// @param to_wrap Object or function to wrap
264/// @return A @c reference_wrapper around the input of const-qualified type
265template <typename T>
266auto cref(T& to_wrap) noexcept -> reference_wrapper<T const> {
267 return reference_wrapper<T const>{to_wrap};
268}
269// parasoft-end-suppress AUTOSAR-M3_3_2-a
270
271/// @brief Create a const type-deduced copy of a @c reference_wrapper
272/// @tparam T Type wrapped by the input wrapper
273/// @param other @c reference_wrapper to copy
274/// @return A copy of the input @c reference_wrapper
275template <typename T>
277 return other;
278}
279
280// parasoft-begin-suppress CERT_C-EXP37-a-3 "False positive: The rule does not mention naming all parameters"
281/// @brief Cannot create a @c reference_wrapper from an rvalue reference
282/// @tparam T type of the reference
283template <typename T>
284auto cref(T&&) = delete;
285// parasoft-end-suppress CERT_C-EXP37-a
286
287} // namespace std
288
289#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_REFERENCE_WRAPPER_HPP_
Arg1 first_argument_type
Type of the wrapped function's first argument.
Definition reference_wrapper.hpp:62
Arg2 second_argument_type
Type of the wrapped function's second argument.
Definition reference_wrapper.hpp:64
Arg argument_type
Type of the wrapped function's argument.
Definition reference_wrapper.hpp:52
Add argument member types to reference wrapped functions and member function pointers....
Definition reference_wrapper.hpp:44
typename T::second_argument_type second_argument_type
Forwarded type from the wrapped class.
Definition reference_wrapper.hpp:124
typename T::first_argument_type first_argument_type
Forwarded type from the wrapped class.
Definition reference_wrapper.hpp:122
Forward member types first_argument_type and second_argument_type from a reference wrapped class cont...
Definition reference_wrapper.hpp:111
typename T::result_type result_type
Forwarded type from the wrapped class.
Definition reference_wrapper.hpp:82
Forward member type result_type from a reference wrapped class containing that member type,...
Definition reference_wrapper.hpp:74
typename T::argument_type argument_type
Forwarded type from the wrapped class.
Definition reference_wrapper.hpp:100
Forward member type argument_type from a reference wrapped class containing that member type,...
Definition reference_wrapper.hpp:92
arene::base::function_return_t< std::remove_pointer_t< T > > result_type
Return type of the wrapped function.
Definition reference_wrapper.hpp:144
arene::base::member_function_return_t< T > result_type
Return type of the wrapped member function pointer.
Definition reference_wrapper.hpp:159
Add member types to reference_wrapper based on the underlying wrapped type.
Definition reference_wrapper.hpp:134
wraps a reference in a copyable and assignable object
Definition reference_wrapper.hpp:178
reference_wrapper(T &&)=delete
Cannot be constructed from an rvalue reference.
auto get() const noexcept -> T &
Access the stored reference.
Definition reference_wrapper.hpp:208
reference_wrapper(T &to_wrap) noexcept
Construct a reference_wrapper around a given input. Wrapped object must have an lvalue reference addr...
Definition reference_wrapper.hpp:198
auto operator()(Args &&... args) const noexcept(arene::base::is_nothrow_invocable_v< T &, Args... >) -> arene::base::invoke_result_t< T &, Args... >
Calls the stored function with given args. Only available if the stored type is callable.
Definition reference_wrapper.hpp:225
T type
Wrapped type.
Definition reference_wrapper.hpp:187
Public export header for the constraints subpackage.
constexpr bool is_invocable_v
Determine if a functor type is invocable with the specified argument types.
typename function_traits< Fn >::return_type function_return_t
Determines the return and argument types of a function.
Definition function_traits.hpp:464
typename invoke_result< Fn, Args... >::type invoke_result_t
The return type of a functor invoked with the specified arguments.
Definition is_invocable.hpp:271
constexpr auto invoke(F &&func, Args &&... args) noexcept(is_nothrow_invocable_v< F, Args... >) ->
Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments...
Definition invoke.hpp:281
constexpr bool is_nothrow_invocable_v
Determine if a functor type is no-throw invocable with the specified argument types.
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
function_return_t< typename member_pointer_traits< F >::pointee_type > member_function_return_t
Obtains the return type of a member function pointer type.
Definition function_traits.hpp:501
Definition accumulate.hpp:36
Definition reference_wrapper.hpp:33
STL namespace.
auto ref(T &to_wrap) noexcept -> reference_wrapper< T >
Create a type-deduced reference_wrapper.
Definition reference_wrapper.hpp:239
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
constexpr auto addressof(T &value) noexcept -> T *
Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
Definition addressof.hpp:24
auto cref(T &to_wrap) noexcept -> reference_wrapper< T const >
Create a const type-deduced reference_wrapper.
Definition reference_wrapper.hpp:266
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