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_TYPE_H 10 #define _LIBCPP___TYPE_TRAITS_COMMON_TYPE_H 11 12 #include <__config> 13 #include <__type_traits/conditional.h> 14 #include <__type_traits/decay.h> 15 #include <__type_traits/is_same.h> 16 #include <__type_traits/remove_cvref.h> 17 #include <__type_traits/void_t.h> 18 #include <__utility/declval.h> 19 20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 21 # pragma GCC system_header 22 #endif 23 24 _LIBCPP_BEGIN_NAMESPACE_STD 25 26 #if _LIBCPP_STD_VER >= 20 27 // Let COND_RES(X, Y) be: 28 template <class _Tp, class _Up> 29 using __cond_type = decltype(false ? std::declval<_Tp>() : std::declval<_Up>()); 30 31 template <class _Tp, class _Up, class = void> 32 struct __common_type3 {}; 33 34 // sub-bullet 4 - "if COND_RES(CREF(D1), CREF(D2)) denotes a type..." 35 template <class _Tp, class _Up> 36 struct __common_type3<_Tp, _Up, void_t<__cond_type<const _Tp&, const _Up&>>> { 37 using type = remove_cvref_t<__cond_type<const _Tp&, const _Up&>>; 38 }; 39 40 template <class _Tp, class _Up, class = void> 41 struct __common_type2_imp : __common_type3<_Tp, _Up> {}; 42 #else 43 template <class _Tp, class _Up, class = void> 44 struct __common_type2_imp {}; 45 #endif 46 47 // sub-bullet 3 - "if decay_t<decltype(false ? declval<D1>() : declval<D2>())> ..." 48 template <class _Tp, class _Up> 49 struct __common_type2_imp<_Tp, _Up, __void_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> > { 50 typedef _LIBCPP_NODEBUG __decay_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> type; 51 }; 52 53 template <class, class = void> 54 struct __common_type_impl {}; 55 56 template <class... _Tp> 57 struct __common_types; 58 template <class... _Tp> 59 struct _LIBCPP_TEMPLATE_VIS common_type; 60 61 template <class _Tp, class _Up> 62 struct __common_type_impl< __common_types<_Tp, _Up>, __void_t<typename common_type<_Tp, _Up>::type> > { 63 typedef typename common_type<_Tp, _Up>::type type; 64 }; 65 66 template <class _Tp, class _Up, class _Vp, class... _Rest> 67 struct __common_type_impl<__common_types<_Tp, _Up, _Vp, _Rest...>, __void_t<typename common_type<_Tp, _Up>::type> > 68 : __common_type_impl<__common_types<typename common_type<_Tp, _Up>::type, _Vp, _Rest...> > {}; 69 70 // bullet 1 - sizeof...(Tp) == 0 71 72 template <> 73 struct _LIBCPP_TEMPLATE_VIS common_type<> {}; 74 75 // bullet 2 - sizeof...(Tp) == 1 76 77 template <class _Tp> 78 struct _LIBCPP_TEMPLATE_VIS common_type<_Tp> : public common_type<_Tp, _Tp> {}; 79 80 // bullet 3 - sizeof...(Tp) == 2 81 82 // sub-bullet 1 - "If is_same_v<T1, D1> is false or ..." 83 template <class _Tp, class _Up> 84 struct _LIBCPP_TEMPLATE_VIS common_type<_Tp, _Up> 85 : __conditional_t<_IsSame<_Tp, __decay_t<_Tp> >::value && _IsSame<_Up, __decay_t<_Up> >::value, 86 __common_type2_imp<_Tp, _Up>, 87 common_type<__decay_t<_Tp>, __decay_t<_Up> > > {}; 88 89 // bullet 4 - sizeof...(Tp) > 2 90 91 template <class _Tp, class _Up, class _Vp, class... _Rest> 92 struct _LIBCPP_TEMPLATE_VIS common_type<_Tp, _Up, _Vp, _Rest...> 93 : __common_type_impl<__common_types<_Tp, _Up, _Vp, _Rest...> > {}; 94 95 #if _LIBCPP_STD_VER >= 14 96 template <class... _Tp> 97 using common_type_t = typename common_type<_Tp...>::type; 98 #endif 99 100 _LIBCPP_END_NAMESPACE_STD 101 102 #endif // _LIBCPP___TYPE_TRAITS_COMMON_TYPE_H 103