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