Warning: This file is not a C or C++ file. It does not have highlighting.

1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H
10#define _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H
11
12#include <__config>
13#include <__type_traits/common_type.h>
14#include <__type_traits/copy_cv.h>
15#include <__type_traits/copy_cvref.h>
16#include <__type_traits/is_convertible.h>
17#include <__type_traits/is_reference.h>
18#include <__type_traits/remove_cvref.h>
19#include <__type_traits/remove_reference.h>
20#include <__utility/declval.h>
21
22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23# pragma GCC system_header
24#endif
25
26_LIBCPP_BEGIN_NAMESPACE_STD
27
28// common_reference
29#if _LIBCPP_STD_VER >= 20
30// Let COND_RES(X, Y) be:
31template <class _Xp, class _Yp>
32using __cond_res _LIBCPP_NODEBUG = decltype(false ? std::declval<_Xp (&)()>()() : std::declval<_Yp (&)()>()());
33
34// Let `XREF(A)` denote a unary alias template `T` such that `T<U>` denotes the same type as `U`
35// with the addition of `A`'s cv and reference qualifiers, for a non-reference cv-unqualified type
36// `U`.
37// [Note: `XREF(A)` is `__xref<A>::template __apply`]
38template <class _Tp>
39struct __xref {
40 template <class _Up>
41 using __apply _LIBCPP_NODEBUG = __copy_cvref_t<_Tp, _Up>;
42};
43
44// Given types A and B, let X be remove_reference_t<A>, let Y be remove_reference_t<B>,
45// and let COMMON-REF(A, B) be:
46template <class _Ap, class _Bp, class _Xp = remove_reference_t<_Ap>, class _Yp = remove_reference_t<_Bp>>
47struct __common_ref;
48
49template <class _Xp, class _Yp>
50using __common_ref_t _LIBCPP_NODEBUG = typename __common_ref<_Xp, _Yp>::__type;
51
52template <class _Xp, class _Yp>
53using __cv_cond_res _LIBCPP_NODEBUG = __cond_res<__copy_cv_t<_Xp, _Yp>&, __copy_cv_t<_Yp, _Xp>&>;
54
55// If A and B are both lvalue reference types, COMMON-REF(A, B) is
56// COND-RES(COPYCV(X, Y)&, COPYCV(Y, X)&) if that type exists and is a reference type.
57// clang-format off
58template <class _Ap, class _Bp, class _Xp, class _Yp>
59 requires
60 requires { typename __cv_cond_res<_Xp, _Yp>; } &&
61 is_reference_v<__cv_cond_res<_Xp, _Yp>>
62struct __common_ref<_Ap&, _Bp&, _Xp, _Yp> {
63 using __type _LIBCPP_NODEBUG = __cv_cond_res<_Xp, _Yp>;
64};
65// clang-format on
66
67// Otherwise, let C be remove_reference_t<COMMON-REF(X&, Y&)>&&. ...
68template <class _Xp, class _Yp>
69using __common_ref_C _LIBCPP_NODEBUG = remove_reference_t<__common_ref_t<_Xp&, _Yp&>>&&;
70
71// .... If A and B are both rvalue reference types, C is well-formed, and
72// is_convertible_v<A, C> && is_convertible_v<B, C> is true, then COMMON-REF(A, B) is C.
73// clang-format off
74template <class _Ap, class _Bp, class _Xp, class _Yp>
75 requires
76 requires { typename __common_ref_C<_Xp, _Yp>; } &&
77 is_convertible_v<_Ap&&, __common_ref_C<_Xp, _Yp>> &&
78 is_convertible_v<_Bp&&, __common_ref_C<_Xp, _Yp>>
79struct __common_ref<_Ap&&, _Bp&&, _Xp, _Yp> {
80 using __type _LIBCPP_NODEBUG = __common_ref_C<_Xp, _Yp>;
81};
82// clang-format on
83
84// Otherwise, let D be COMMON-REF(const X&, Y&). ...
85template <class _Tp, class _Up>
86using __common_ref_D _LIBCPP_NODEBUG = __common_ref_t<const _Tp&, _Up&>;
87
88// ... If A is an rvalue reference and B is an lvalue reference and D is well-formed and
89// is_convertible_v<A, D> is true, then COMMON-REF(A, B) is D.
90// clang-format off
91template <class _Ap, class _Bp, class _Xp, class _Yp>
92 requires
93 requires { typename __common_ref_D<_Xp, _Yp>; } &&
94 is_convertible_v<_Ap&&, __common_ref_D<_Xp, _Yp>>
95struct __common_ref<_Ap&&, _Bp&, _Xp, _Yp> {
96 using __type _LIBCPP_NODEBUG = __common_ref_D<_Xp, _Yp>;
97};
98// clang-format on
99
100// Otherwise, if A is an lvalue reference and B is an rvalue reference, then
101// COMMON-REF(A, B) is COMMON-REF(B, A).
102template <class _Ap, class _Bp, class _Xp, class _Yp>
103struct __common_ref<_Ap&, _Bp&&, _Xp, _Yp> : __common_ref<_Bp&&, _Ap&> {};
104
105// Otherwise, COMMON-REF(A, B) is ill-formed.
106template <class _Ap, class _Bp, class _Xp, class _Yp>
107struct __common_ref {};
108
109// Note C: For the common_reference trait applied to a parameter pack [...]
110
111template <class...>
112struct _LIBCPP_NO_SPECIALIZATIONS common_reference;
113
114template <class... _Types>
115using common_reference_t = typename common_reference<_Types...>::type;
116
117template <class, class, template <class> class, template <class> class>
118struct basic_common_reference {};
119
120_LIBCPP_DIAGNOSTIC_PUSH
121# if __has_warning("-Winvalid-specialization")
122_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-specialization")
123# endif
124// bullet 1 - sizeof...(T) == 0
125template <>
126struct common_reference<> {};
127
128// bullet 2 - sizeof...(T) == 1
129template <class _Tp>
130struct common_reference<_Tp> {
131 using type _LIBCPP_NODEBUG = _Tp;
132};
133
134// bullet 3 - sizeof...(T) == 2
135template <class _Tp, class _Up>
136struct __common_reference_sub_bullet3;
137template <class _Tp, class _Up>
138struct __common_reference_sub_bullet2 : __common_reference_sub_bullet3<_Tp, _Up> {};
139template <class _Tp, class _Up>
140struct __common_reference_sub_bullet1 : __common_reference_sub_bullet2<_Tp, _Up> {};
141
142// sub-bullet 1 - If T1 and T2 are reference types and COMMON-REF(T1, T2) is well-formed, then
143// the member typedef `type` denotes that type.
144template <class _Tp, class _Up>
145struct common_reference<_Tp, _Up> : __common_reference_sub_bullet1<_Tp, _Up> {};
146
147template <class _Tp, class _Up>
148 requires is_reference_v<_Tp> && is_reference_v<_Up> && requires { typename __common_ref_t<_Tp, _Up>; }
149struct __common_reference_sub_bullet1<_Tp, _Up> {
150 using type _LIBCPP_NODEBUG = __common_ref_t<_Tp, _Up>;
151};
152
153// sub-bullet 2 - Otherwise, if basic_common_reference<remove_cvref_t<T1>, remove_cvref_t<T2>, XREF(T1), XREF(T2)>::type
154// is well-formed, then the member typedef `type` denotes that type.
155template <class _Tp, class _Up>
156using __basic_common_reference_t _LIBCPP_NODEBUG =
157 typename basic_common_reference<remove_cvref_t<_Tp>,
158 remove_cvref_t<_Up>,
159 __xref<_Tp>::template __apply,
160 __xref<_Up>::template __apply>::type;
161
162template <class _Tp, class _Up>
163 requires requires { typename __basic_common_reference_t<_Tp, _Up>; }
164struct __common_reference_sub_bullet2<_Tp, _Up> {
165 using type _LIBCPP_NODEBUG = __basic_common_reference_t<_Tp, _Up>;
166};
167
168// sub-bullet 3 - Otherwise, if COND-RES(T1, T2) is well-formed,
169// then the member typedef `type` denotes that type.
170template <class _Tp, class _Up>
171 requires requires { typename __cond_res<_Tp, _Up>; }
172struct __common_reference_sub_bullet3<_Tp, _Up> {
173 using type _LIBCPP_NODEBUG = __cond_res<_Tp, _Up>;
174};
175
176// sub-bullet 4 & 5 - Otherwise, if common_type_t<T1, T2> is well-formed,
177// then the member typedef `type` denotes that type.
178// - Otherwise, there shall be no member `type`.
179template <class _Tp, class _Up>
180struct __common_reference_sub_bullet3 : common_type<_Tp, _Up> {};
181
182// bullet 4 - If there is such a type `C`, the member typedef type shall denote the same type, if
183// any, as `common_reference_t<C, Rest...>`.
184template <class _Tp, class _Up, class _Vp, class... _Rest>
185 requires requires { typename common_reference_t<_Tp, _Up>; }
186struct common_reference<_Tp, _Up, _Vp, _Rest...> : common_reference<common_reference_t<_Tp, _Up>, _Vp, _Rest...> {};
187_LIBCPP_DIAGNOSTIC_POP
188
189// bullet 5 - Otherwise, there shall be no member `type`.
190template <class...>
191struct _LIBCPP_NO_SPECIALIZATIONS common_reference {};
192
193#endif // _LIBCPP_STD_VER >= 20
194
195_LIBCPP_END_NAMESPACE_STD
196
197#endif // _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H
198

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libcxx/include/__type_traits/common_reference.h