Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
common_reference.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "Header defines a group of traits, so the name reflects that"
2// Copyright 2024, Toyota Motor Corporation
3//
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5
6#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMMON_REFERENCE_HPP_
7#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMMON_REFERENCE_HPP_
8
9// IWYU pragma: private, include "arene/base/type_traits.hpp"
10// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
11
14#include "arene/base/stdlib_choice/add_pointer.hpp"
15#include "arene/base/stdlib_choice/common_type.hpp"
16#include "arene/base/stdlib_choice/declval.hpp"
17#include "arene/base/stdlib_choice/enable_if.hpp"
18#include "arene/base/stdlib_choice/integral_constant.hpp"
19#include "arene/base/stdlib_choice/is_convertible.hpp"
20#include "arene/base/stdlib_choice/is_reference.hpp"
21#include "arene/base/stdlib_choice/is_same.hpp"
22#include "arene/base/stdlib_choice/remove_cv.hpp"
23#include "arene/base/stdlib_choice/remove_reference.hpp"
27
28// IWYU pragma: no_include "arene/base/type_manipulation/static_if.hpp"
29
30namespace arene {
31namespace base {
32
33/// @brief A customization point to allow defining a common reference type for two types.
34/// @tparam T The first type for which to define a common reference.
35/// @tparam U The second type for which to define a common reference.
36/// @tparam TQual A template that applies cv-qualifiers to T.
37/// @tparam UQual A template that applies cv-qualifiers to U.
38///
39/// A specialization of this trait must be a partial specialization across @c T and @c U and must satisfy the following
40/// constraints:
41/// - @c std::is_same<T,decay_t<T>>::value must be true.
42/// - @c std::is_same<U,decay_t<U>>::value must be true.
43/// - At least one of @c T or @c U must be a user-defined type.
44///
45/// If there is a common reference type between the two types, the specialization must define a member type named @c
46/// type that satisfies the following constraints:
47/// - @c TQual<T>::type must be convertible to @c type.
48/// - @c UQual<U>::type must be convertible to @c type.
49///
50/// In addition, @c basic_common_reference<T,U,TQual,UQual>::type and @c basic_common_reference<U,T,UQual,TQual>::type
51/// must be the same type or both must not define a member type named @c type .
52template <class T, class U, template <class> class TQual, template <class> class UQual>
54
55/// @brief A trait which determines the common reference type for a set of types.
56/// @tparam Ts The types for which to determine the common reference.
57///
58/// The _common reference type_ is defined as follows:
59/// - If @c sizeof...(Ts) is 0, @c type is not defined.
60/// - If @c sizeof...(Ts) is 1, @c type names the only type in the pack.
61/// - If @c sizeof...(Ts) is 2, the following alternatives are considered in order:
62/// - Let @c R be @c COMMON-REF(T0,T1). If @c T0 and @c T1 are reference types, @c R is well-formed, and both
63/// pointer-conversion checks to @c R succeed, @c type names @c R.
64/// - Otherwise, if @c basic_common_reference<remove_cvref_t<T0>,remove_cvref_t<T1>,T0Qual,T1Qual>::type is defined,
65/// @c type names that type.
66/// - Otherwise, if @c COND-RES(T0,T1) is well-formed, @c type names that type.
67/// - Otherwise, if @c std::common_type_t<T0,T1> is well-formed, @c type names that type.
68/// - Otherwise, @c type is not defined.
69/// - If @c sizeof...(Ts) is greater than 2, let @c C be @c common_reference_t<T0,T1>. If @c C is well-formed, @c type
70/// names the type of @c common_reference<C,Ts...>; otherwise, @c type is not defined.
71///
72/// The exposition-only @c COMMON-REF(A,B) operation is defined as follows. Let @c X and @c Y be
73/// the results of applying @c remove_reference_t to @c A and @c B, respectively. Let @c CREF(A) be a const lvalue
74/// reference to @c X, @c COPYCV(FROM,TO) be @c TO with the top-level cv-qualifiers of @c FROM, and
75/// @c COND-RES(X,Y) be <c>decltype(false ? std::declval<X(&)()>()() : std::declval<Y(&)()>()())</c>.
76/// - If @c A and @c B are both lvalue reference types, @c COMMON-REF(A,B) is
77/// @c COND-RES(COPYCV(X,Y)&,COPYCV(Y,X)&) if that type exists and is a reference type.
78/// - If @c A and @c B are both rvalue reference types, let @c C be
79/// @c remove_reference_t<COMMON-REF(X&,Y&)>&&. If @c C is well-formed and both types are convertible to @c C,
80/// @c COMMON-REF(A,B) is @c C.
81/// - If @c A is an rvalue reference and @c B is an lvalue reference, let @c D be @c COMMON-REF(CREF(X),Y&). If
82/// @c D is well-formed and @c A is convertible to @c D, @c COMMON-REF(A,B) is @c D.
83/// - If @c A is an lvalue reference and @c B is an rvalue reference, @c COMMON-REF(A,B) is @c COMMON-REF(B,A).
84/// - Otherwise, @c COMMON-REF(A,B) is not defined.
85template <typename... Ts>
87
88/// @brief Specialization of @c common_reference for a single type.
89/// @tparam T0 The single type for which to determine the common reference.
90/// @see common_reference<Ts...>
91template <typename T0>
93 public:
94 /// @brief The common reference type of a single type, which is itself.
95 using type = T0;
96};
97
98/// @brief Alias template to extract the common reference type for a set of types.
99/// @tparam Ts The types for which to determine the common reference.
100/// Equivalent to @c common_reference<Ts...>::type .
101template <typename... Ts>
102using common_reference_t = typename common_reference<Ts...>::type;
103
104namespace common_reference_detail {
105
106/// @brief Alias trait for @c basic_common_reference<...>::type
107///
108/// @tparam T The first type for which to define a common reference.
109/// @tparam U The second type for which to define a common reference.
110/// @tparam TQual A template that applies cv-qualifiers to T.
111/// @tparam UQual A template that applies cv-qualifiers to U.
112/// @see basic_common_reference<T,U,TQual,UQual>
113template <class T, class U, template <class> class TQual, template <class> class UQual>
114using basic_common_reference_t = typename basic_common_reference<T, U, TQual, UQual>::type;
115
116// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
117/// @brief Helper struct to deduce the common reference type of two types through overload resolution.
118class common_ref_deducer {
119 public:
120 /// @brief Helper alias template to deduce the common reference type of two types.
121 /// @tparam X The first type. Must be a non-reference
122 /// @tparam Y The second type. Must be a non-reference
123 /// @tparam CVUX The type X with the cv-qualifiers of Y applied.
124 /// @tparam CVUY The type Y with the cv-qualifiers of X applied.
125 /// @return The common reference type of X and Y if it is defined.
126 template <typename X, typename Y, typename CVUX = give_qualifiers_to<Y, X>, typename CVUY = give_qualifiers_to<X, Y>>
127 using common_ref_t = decltype(false ? std::declval<CVUX&>() : std::declval<CVUY&>());
128
129 // parasoft-begin-suppress AUTOSAR-A13_3_1-a "False positive: Constrained via SFINAE"
130 /// @brief Helper function selected when both arguments are lvalue references.
131 /// @tparam X The first non-reference type.
132 /// @tparam Y The second non-reference type.
133 /// @return The common reference type of X and Y if it is defined.
134 /// @see common_reference
135 template <typename X, typename Y>
136 static constexpr auto type(X&, Y&) -> common_ref_deducer::common_ref_t<X, Y>;
137
138 /// @brief Helper function selected when both arguments are rvalue references.
139 /// @tparam X The first non-reference type.
140 /// @tparam Y The second non-reference type.
141 /// @tparam C The deduced common rvalue reference type.
142 /// @return @c C if both arguments are convertible to it.
143 /// @see common_reference
144 template <
145 typename X,
146 typename Y,
151 static constexpr auto type(X&&, Y&&) -> C;
152
153 /// @brief Helper function selected when the first argument is an lvalue reference and the second is an rvalue
154 /// reference.
155 /// @tparam X The first non-reference type.
156 /// @tparam Y The second non-reference type.
157 /// @tparam D The deduced common reference type.
158 /// @return @c D if the second argument is convertible to it.
159 /// @see common_reference
160 template <
161 typename X,
162 typename Y,
163 typename D = common_ref_deducer::common_ref_t<X, Y const>,
165 static constexpr auto type(X&, Y&&) -> D;
166
167 /// @brief Helper function selected when the first argument is an rvalue reference and the second is an lvalue
168 /// reference.
169 /// @tparam X The first non-reference type.
170 /// @tparam Y The second non-reference type.
171 /// @tparam D The deduced common reference type.
172 /// @return @c D if the first argument is convertible to it.
173 /// @see common_reference
174 template <
175 typename X,
176 typename Y,
177 typename D = common_ref_deducer::common_ref_t<Y, X const>,
179 static constexpr auto type(X&&, Y&) -> D;
180 // parasoft-end-suppress AUTOSAR-A13_3_1-a "False positive: Constrained via SFINAE"
181};
182// parasoft-end-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
183
184/// @brief Alias template to determine the common reference type for two types via @c common_ref_deducer .
185/// @tparam T0 The first type. Must be a reference type.
186/// @tparam T1 The second type. Must be a reference type.
187/// @return The common reference type of T0 and T1 if it is defined, otherwise malformed.
188template <typename T0, typename T1>
189using common_ref_t = decltype(common_ref_deducer::type(std::declval<T0>(), std::declval<T1>()));
190
191/// @brief Exposition-only helper that provides the COPYCV operation for a source type.
192/// @tparam Source The source type from which to extract cv-qualifiers.
193template <typename Source>
194class copy_cv {
195 public:
196 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: identifier 'apply' does not hide anything"
197 /// @brief Template alias to apply the cv-qualifiers of the source type to another type.
198 /// @tparam T The type to which the cv-qualifiers of @c Source will be applied.
199 template <typename T>
200 using apply = give_qualifiers_to<Source, T>;
201 // parasoft-end-suppress AUTOSAR-A2_10_1-d "False positive: identifier 'apply' does not hide anything"
202};
203
204// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
205/// @brief Helper struct to deduce the common reference type of two types. It contains overloads of @c type<T0,T1> whose
206/// return type is the common reference type if it is defined. If it is not defined, then @c type<T0,T1> does not
207/// participate in overload resolution.
208
209class common_reference_2deducer {
210 public:
211 /// @brief Overload of @c type for when both T0 and T1 are reference types and a common reference type can be deduced.
212 /// @tparam T0 The first type.
213 /// @tparam T1 The second type.
214 /// @tparam CommonReferenceT The common reference type deduced for T0 and T1.
215 /// @return The common reference type of T0 and T1
216 template <
217 typename T0,
218 typename T1,
219 typename CommonReferenceT = common_ref_t<T0, T1>,
224 nullptr>
225 static auto type(priority_tag<3U>) -> CommonReferenceT;
226
227 /// @brief Overload of @c type for when the type pairing has a specialization of @c basic_common_reference .
228 /// @tparam T0 The first type.
229 /// @tparam T1 The second type.
230 /// @return The common reference type as defined by @c basic_common_reference for the given type pairing.
231 template <typename T0, typename T1>
232 static auto type(priority_tag<2U>) -> basic_common_reference_t<
235 copy_cv<T0>::template apply,
236 copy_cv<T1>::template apply>;
237
238 /// @brief Overload of @c type for when the type pairing does not have a common reference type or a specialization of
239 /// @c basic_common_reference but the common reference can be deduced by implicit conversion.
240 /// @tparam T0 The first type.
241 /// @tparam T1 The second type.
242 /// @return The common reference type as deduced by the conditional operator.
243 template <typename T0, typename T1>
244 static auto type(priority_tag<1U>) -> decltype(false ? std::declval<T0 (&)()>()() : std::declval<T1 (&)()>()());
245
246 /// @brief Overload of @c type for when the type pairing does not have a common reference type, a specialization of @c
247 /// basic_common_reference, and cannot be deduced by implicit conversion.
248 /// @tparam T0 The first type.
249 /// @tparam T1 The second type.
250 /// @return The common reference type as determined by @c std::common_type.
251 template <typename T0, typename T1>
252 static auto type(priority_tag<0U>) -> std::common_type_t<T0, T1>;
253};
254// parasoft-end-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
255
256/// @brief Alias template for deducing the common reference type of two types using @c common_reference_2deducer.
257/// @tparam T0 The first type.
258/// @tparam T1 The second type.
259/// @return The common reference type of T0 and T1 if it is defined, otherwise malformed.
260template <typename T0, typename T1>
261using common_reference_2deducer_t = decltype(common_reference_2deducer::type<T0, T1>(priority_tag<3U>{}));
262
263/// @brief Implementation detail for determining the common reference type for two types using @c
264/// common_reference_2deducer_t.
265/// @tparam T0 The first type.
266/// @tparam T1 The second type.
267/// If a common reference exists for T0 and T1, this class will have a member typedef @c type representing that common
268/// reference, otherwise it will have no member typedef defined.
269template <typename T0, typename T1, typename = constraints<>>
270class common_reference_2impl {};
271
272/// @brief Specialization of @c common_reference_2impl when a common reference exists for two types.
273/// @tparam T0 The first type.
274/// @tparam T1 The second type.
275template <typename T0, typename T1>
276class common_reference_2impl<T0, T1, constraints<common_reference_2deducer_t<T0, T1>>> {
277 public:
278 /// @brief The common reference type of T0 and T1 as determined by @c common_reference_2deducer_t.
279 using type = common_reference_2deducer_t<T0, T1>;
280};
281
282/// @brief Implementation detail for determining the common reference type for three or more types.
283/// @tparam CommonInitialReference Indicates whether a common reference exists for the initial two types.
284/// @tparam T0 The first type.
285/// @tparam T1 The second type.
286/// @tparam Ts The remaining types.
287/// The default implementation does not define a member typedef @c type
288template <bool CommonInitialReference, typename T0, typename T1, typename... Ts>
289class common_reference_3impl {};
290
291/// @brief Specialization of @c common_reference_3impl when a common reference exists for the initial two types.
292/// @tparam T0 The first type.
293/// @tparam T1 The second type.
294/// @tparam Ts The remaining types.
295template <typename T0, typename T1, typename... Ts>
296class common_reference_3impl<true, T0, T1, Ts...>
297 : public common_reference<common_reference_2deducer_t<T0, T1>, Ts...> {};
298} // namespace common_reference_detail
299
300/// @brief Specialization of @c common_reference for two types.
301/// @tparam T0 The first type for which to determine the common reference.
302/// @tparam T1 The second type for which to determine the common reference.
303/// @see common_reference<Ts...>
304template <typename T0, typename T1>
305class common_reference<T0, T1> : public common_reference_detail::common_reference_2impl<T0, T1> {};
306
307/// @brief Specialization of @c common_reference for more than two types.
308/// @tparam T0 The first type for which to determine the common reference.
309/// @tparam T1 The second type for which to determine the common reference.
310/// @tparam Ts The remaining types for which to determine the common reference.
311/// @see common_reference<Ts...>
312template <typename T0, typename T1, typename... Ts>
313class common_reference<T0, T1, Ts...>
314 : public common_reference_detail::common_reference_3impl<
315 substitution_succeeds<common_reference_detail::common_reference_2deducer_t, T0, T1>,
316 T0,
317 T1,
318 Ts...> {};
319
320namespace common_reference_with_detail {
321/// @brief Trait to determine if two types have a common reference.
322/// @tparam T The first type.
323/// @tparam U The second type.
324/// The default implementation is false
325template <typename T, typename U, typename = constraints<>>
326class has_common_reference_with : public std::false_type {};
327
328/// @brief Specialization of @c has_common_reference_with for types that satisfy the common reference constraints.
329/// @tparam T The first type.
330/// @tparam U The second type.
331template <typename T, typename U>
332class has_common_reference_with<
333 T,
334 U,
336 std::enable_if_t<std::is_same<common_reference_t<T, U>, common_reference_t<U, T>>::value>,
337 std::enable_if_t<std::is_convertible<T, common_reference_t<T, U>>::value>,
338 std::enable_if_t<std::is_convertible<U, common_reference_t<T, U>>::value>>> : public std::true_type {};
339} // namespace common_reference_with_detail
340
341/// @brief Query trait to determine if two types have a common reference. A backport of the
342/// @c [std::common_reference_with](https://en.cppreference.com/cpp/concepts/common_reference_with) concept.
343/// @tparam T The first type.
344/// @tparam U The second type.
345/// @return bool @c true if all of the following are satisfied, else @c false :
346/// - @c common_reference_t<T,U> is the same as @c common_reference_t<U,T>.
347/// - @c T is convertible to @c common_reference_t<T,U>.
348/// - @c U is convertible to @c common_reference_t<T,U>.
349/// @note The semantic requirement of this trait is that conversion to the common reference type must be _equality
350/// perserving_.
351template <typename T, typename U>
352extern constexpr bool has_common_reference_with_v =
354
355} // namespace base
356} // namespace arene
357
358#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_TRAITS_COMMON_REFERENCE_HPP_
A customization point to allow defining a common reference type for two types.
Definition common_reference.hpp:53
T0 type
The common reference type of a single type, which is itself.
Definition common_reference.hpp:95
A trait which determines the common reference type for a set of types.
Definition common_reference.hpp:86
A priority tag for overload resolution.
Definition priority_tag.hpp:45
static constexpr value_type value
Definition integral_constant.hpp:34
Definition accumulate.hpp:37
constexpr bool has_common_reference_with_v
Query trait to determine if two types have a common reference. A backport of the [std::common_referen...
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 static_if< give_qualifiers_to_detail::is_volatile_v< Source > >::template then_apply_else_apply< give_qualifiers_to_detail::give_qualifiers_to_impl_volatile, give_qualifiers_to_detail::give_qualifiers_to_impl_nonvolatile, Source, Target > give_qualifiers_to
For a type Source and type Target: if Source is const volatile results in Target const volatile other...
Definition give_qualifiers_to.hpp:68
typename common_reference< Ts... >::type common_reference_t
Alias template to extract the common reference type for a set of types.
Definition common_reference.hpp:102
Definition accumulate.hpp:36
STL namespace.
typename common_type< Ts... >::type common_type_t
Helper alias template for common_type.
Definition common_type.hpp:99
bool_constant< false > false_type
An alias for bool_constant<false>
Definition integral_constant.hpp:79
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
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
auto declval() noexcept -> T &&
A dummy function that is declared but not defined, with a user-specified return type,...
typename remove_reference< T >::type remove_reference_t
An alias for T, but with top-level reference qualifiers removed.
Definition remove_reference.hpp:47
typename add_pointer< Type >::type add_pointer_t
Adds a pointer to the given type.
Definition add_pointer.hpp:62