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: 31 template <class _Xp, class _Yp> 32 using __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`] 38 template <class _Tp> 39 struct __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: 46 template <class _Ap, class _Bp, class _Xp = remove_reference_t<_Ap>, class _Yp = remove_reference_t<_Bp>> 47 struct __common_ref; 48 49 template <class _Xp, class _Yp> 50 using __common_ref_t _LIBCPP_NODEBUG = typename __common_ref<_Xp, _Yp>::__type; 51 52 template <class _Xp, class _Yp> 53 using __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 58 template <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>> 62 struct __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&)>&&. ... 68 template <class _Xp, class _Yp> 69 using __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 74 template <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>> 79 struct __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&). ... 85 template <class _Tp, class _Up> 86 using __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 91 template <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>> 95 struct __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). 102 template <class _Ap, class _Bp, class _Xp, class _Yp> 103 struct __common_ref<_Ap&, _Bp&&, _Xp, _Yp> : __common_ref<_Bp&&, _Ap&> {}; 104 105 // Otherwise, COMMON-REF(A, B) is ill-formed. 106 template <class _Ap, class _Bp, class _Xp, class _Yp> 107 struct __common_ref {}; 108 109 // Note C: For the common_reference trait applied to a parameter pack [...] 110 111 template <class...> 112 struct common_reference; 113 114 template <class... _Types> 115 using common_reference_t = typename common_reference<_Types...>::type; 116 117 // bullet 1 - sizeof...(T) == 0 118 template <> 119 struct common_reference<> {}; 120 121 // bullet 2 - sizeof...(T) == 1 122 template <class _Tp> 123 struct common_reference<_Tp> { 124 using type = _Tp; 125 }; 126 127 // bullet 3 - sizeof...(T) == 2 128 template <class _Tp, class _Up> 129 struct __common_reference_sub_bullet3; 130 template <class _Tp, class _Up> 131 struct __common_reference_sub_bullet2 : __common_reference_sub_bullet3<_Tp, _Up> {}; 132 template <class _Tp, class _Up> 133 struct __common_reference_sub_bullet1 : __common_reference_sub_bullet2<_Tp, _Up> {}; 134 135 // sub-bullet 1 - If T1 and T2 are reference types and COMMON-REF(T1, T2) is well-formed, then 136 // the member typedef `type` denotes that type. 137 template <class _Tp, class _Up> 138 struct common_reference<_Tp, _Up> : __common_reference_sub_bullet1<_Tp, _Up> {}; 139 140 template <class _Tp, class _Up> 141 requires is_reference_v<_Tp> && is_reference_v<_Up> && requires { typename __common_ref_t<_Tp, _Up>; } 142 struct __common_reference_sub_bullet1<_Tp, _Up> { 143 using type = __common_ref_t<_Tp, _Up>; 144 }; 145 146 // sub-bullet 2 - Otherwise, if basic_common_reference<remove_cvref_t<T1>, remove_cvref_t<T2>, XREF(T1), XREF(T2)>::type 147 // is well-formed, then the member typedef `type` denotes that type. 148 template <class, class, template <class> class, template <class> class> 149 struct basic_common_reference {}; 150 151 template <class _Tp, class _Up> 152 using __basic_common_reference_t _LIBCPP_NODEBUG = 153 typename basic_common_reference<remove_cvref_t<_Tp>, 154 remove_cvref_t<_Up>, 155 __xref<_Tp>::template __apply, 156 __xref<_Up>::template __apply>::type; 157 158 template <class _Tp, class _Up> 159 requires requires { typename __basic_common_reference_t<_Tp, _Up>; } 160 struct __common_reference_sub_bullet2<_Tp, _Up> { 161 using type = __basic_common_reference_t<_Tp, _Up>; 162 }; 163 164 // sub-bullet 3 - Otherwise, if COND-RES(T1, T2) is well-formed, 165 // then the member typedef `type` denotes that type. 166 template <class _Tp, class _Up> 167 requires requires { typename __cond_res<_Tp, _Up>; } 168 struct __common_reference_sub_bullet3<_Tp, _Up> { 169 using type = __cond_res<_Tp, _Up>; 170 }; 171 172 // sub-bullet 4 & 5 - Otherwise, if common_type_t<T1, T2> is well-formed, 173 // then the member typedef `type` denotes that type. 174 // - Otherwise, there shall be no member `type`. 175 template <class _Tp, class _Up> 176 struct __common_reference_sub_bullet3 : common_type<_Tp, _Up> {}; 177 178 // bullet 4 - If there is such a type `C`, the member typedef type shall denote the same type, if 179 // any, as `common_reference_t<C, Rest...>`. 180 template <class _Tp, class _Up, class _Vp, class... _Rest> 181 requires requires { typename common_reference_t<_Tp, _Up>; } 182 struct common_reference<_Tp, _Up, _Vp, _Rest...> : common_reference<common_reference_t<_Tp, _Up>, _Vp, _Rest...> {}; 183 184 // bullet 5 - Otherwise, there shall be no member `type`. 185 template <class...> 186 struct common_reference {}; 187 188 #endif // _LIBCPP_STD_VER >= 20 189 190 _LIBCPP_END_NAMESPACE_STD 191 192 #endif // _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H 193