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_cv.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 > 17 31 // Let COND_RES(X, Y) be: 32 template <class _Xp, class _Yp> 33 using __cond_res = 34 decltype(false ? declval<_Xp(&)()>()() : declval<_Yp(&)()>()()); 35 36 // Let `XREF(A)` denote a unary alias template `T` such that `T<U>` denotes the same type as `U` 37 // with the addition of `A`'s cv and reference qualifiers, for a non-reference cv-unqualified type 38 // `U`. 39 // [Note: `XREF(A)` is `__xref<A>::template __apply`] 40 template <class _Tp> 41 struct __xref { 42 template<class _Up> 43 using __apply = __copy_cvref_t<_Tp, _Up>; 44 }; 45 46 // Given types A and B, let X be remove_reference_t<A>, let Y be remove_reference_t<B>, 47 // and let COMMON-REF(A, B) be: 48 template<class _Ap, class _Bp, class _Xp = remove_reference_t<_Ap>, class _Yp = remove_reference_t<_Bp>> 49 struct __common_ref; 50 51 template<class _Xp, class _Yp> 52 using __common_ref_t = typename __common_ref<_Xp, _Yp>::__type; 53 54 template<class _Xp, class _Yp> 55 using __cv_cond_res = __cond_res<__copy_cv_t<_Xp, _Yp>&, __copy_cv_t<_Yp, _Xp>&>; 56 57 58 // If A and B are both lvalue reference types, COMMON-REF(A, B) is 59 // COND-RES(COPYCV(X, Y)&, COPYCV(Y, X)&) if that type exists and is a reference type. 60 template<class _Ap, class _Bp, class _Xp, class _Yp> 61 requires requires { typename __cv_cond_res<_Xp, _Yp>; } && is_reference_v<__cv_cond_res<_Xp, _Yp>> 62 struct __common_ref<_Ap&, _Bp&, _Xp, _Yp> 63 { 64 using __type = __cv_cond_res<_Xp, _Yp>; 65 }; 66 67 // Otherwise, let C be remove_reference_t<COMMON-REF(X&, Y&)>&&. ... 68 template <class _Xp, class _Yp> 69 using __common_ref_C = remove_reference_t<__common_ref_t<_Xp&, _Yp&>>&&; 70 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 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 { 81 using __type = __common_ref_C<_Xp, _Yp>; 82 }; 83 84 // Otherwise, let D be COMMON-REF(const X&, Y&). ... 85 template <class _Tp, class _Up> 86 using __common_ref_D = __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 template<class _Ap, class _Bp, class _Xp, class _Yp> 91 requires requires { typename __common_ref_D<_Xp, _Yp>; } && 92 is_convertible_v<_Ap&&, __common_ref_D<_Xp, _Yp>> 93 struct __common_ref<_Ap&&, _Bp&, _Xp, _Yp> 94 { 95 using __type = __common_ref_D<_Xp, _Yp>; 96 }; 97 98 // Otherwise, if A is an lvalue reference and B is an rvalue reference, then 99 // COMMON-REF(A, B) is COMMON-REF(B, A). 100 template<class _Ap, class _Bp, class _Xp, class _Yp> 101 struct __common_ref<_Ap&, _Bp&&, _Xp, _Yp> : __common_ref<_Bp&&, _Ap&> {}; 102 103 // Otherwise, COMMON-REF(A, B) is ill-formed. 104 template<class _Ap, class _Bp, class _Xp, class _Yp> 105 struct __common_ref {}; 106 107 // Note C: For the common_reference trait applied to a parameter pack [...] 108 109 template <class...> 110 struct common_reference; 111 112 template <class... _Types> 113 using common_reference_t = typename common_reference<_Types...>::type; 114 115 // bullet 1 - sizeof...(T) == 0 116 template<> 117 struct common_reference<> {}; 118 119 // bullet 2 - sizeof...(T) == 1 120 template <class _Tp> 121 struct common_reference<_Tp> 122 { 123 using type = _Tp; 124 }; 125 126 // bullet 3 - sizeof...(T) == 2 127 template <class _Tp, class _Up> struct __common_reference_sub_bullet3; 128 template <class _Tp, class _Up> struct __common_reference_sub_bullet2 : __common_reference_sub_bullet3<_Tp, _Up> {}; 129 template <class _Tp, class _Up> struct __common_reference_sub_bullet1 : __common_reference_sub_bullet2<_Tp, _Up> {}; 130 131 // sub-bullet 1 - If T1 and T2 are reference types and COMMON-REF(T1, T2) is well-formed, then 132 // the member typedef `type` denotes that type. 133 template <class _Tp, class _Up> struct common_reference<_Tp, _Up> : __common_reference_sub_bullet1<_Tp, _Up> {}; 134 135 template <class _Tp, class _Up> 136 requires is_reference_v<_Tp> && is_reference_v<_Up> && requires { typename __common_ref_t<_Tp, _Up>; } 137 struct __common_reference_sub_bullet1<_Tp, _Up> 138 { 139 using type = __common_ref_t<_Tp, _Up>; 140 }; 141 142 // sub-bullet 2 - Otherwise, if basic_common_reference<remove_cvref_t<T1>, remove_cvref_t<T2>, XREF(T1), XREF(T2)>::type 143 // is well-formed, then the member typedef `type` denotes that type. 144 template <class, class, template <class> class, template <class> class> struct basic_common_reference {}; 145 146 template <class _Tp, class _Up> 147 using __basic_common_reference_t = typename basic_common_reference< 148 remove_cvref_t<_Tp>, remove_cvref_t<_Up>, 149 __xref<_Tp>::template __apply, __xref<_Up>::template __apply>::type; 150 151 template <class _Tp, class _Up> 152 requires requires { typename __basic_common_reference_t<_Tp, _Up>; } 153 struct __common_reference_sub_bullet2<_Tp, _Up> 154 { 155 using type = __basic_common_reference_t<_Tp, _Up>; 156 }; 157 158 // sub-bullet 3 - Otherwise, if COND-RES(T1, T2) is well-formed, 159 // then the member typedef `type` denotes that type. 160 template <class _Tp, class _Up> 161 requires requires { typename __cond_res<_Tp, _Up>; } 162 struct __common_reference_sub_bullet3<_Tp, _Up> 163 { 164 using type = __cond_res<_Tp, _Up>; 165 }; 166 167 168 // sub-bullet 4 & 5 - Otherwise, if common_type_t<T1, T2> is well-formed, 169 // then the member typedef `type` denotes that type. 170 // - Otherwise, there shall be no member `type`. 171 template <class _Tp, class _Up> struct __common_reference_sub_bullet3 : common_type<_Tp, _Up> {}; 172 173 // bullet 4 - If there is such a type `C`, the member typedef type shall denote the same type, if 174 // any, as `common_reference_t<C, Rest...>`. 175 template <class _Tp, class _Up, class _Vp, class... _Rest> 176 requires requires { typename common_reference_t<_Tp, _Up>; } 177 struct common_reference<_Tp, _Up, _Vp, _Rest...> 178 : common_reference<common_reference_t<_Tp, _Up>, _Vp, _Rest...> 179 {}; 180 181 // bullet 5 - Otherwise, there shall be no member `type`. 182 template <class...> struct common_reference {}; 183 184 #endif // _LIBCPP_STD_VER > 17 185 186 _LIBCPP_END_NAMESPACE_STD 187 188 #endif // _LIBCPP___TYPE_TRAITS_COMMON_REFERENCE_H 189