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_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/type_identity.h> |
18 | #include <__type_traits/void_t.h> |
19 | #include <__utility/declval.h> |
20 | #include <__utility/empty.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 | #if __has_builtin(__builtin_common_type) |
29 | |
30 | template <class... _Args> |
31 | struct common_type; |
32 | |
33 | template <class... _Args> |
34 | using __common_type_t _LIBCPP_NODEBUG = typename common_type<_Args...>::type; |
35 | |
36 | template <class... _Args> |
37 | struct common_type : __builtin_common_type<__common_type_t, __type_identity, __empty, _Args...> {}; |
38 | |
39 | #else |
40 | # if _LIBCPP_STD_VER >= 20 |
41 | // Let COND_RES(X, Y) be: |
42 | template <class _Tp, class _Up> |
43 | using __cond_type _LIBCPP_NODEBUG = decltype(false ? std::declval<_Tp>() : std::declval<_Up>()); |
44 | |
45 | template <class _Tp, class _Up, class = void> |
46 | struct __common_type3 {}; |
47 | |
48 | // sub-bullet 4 - "if COND_RES(CREF(D1), CREF(D2)) denotes a type..." |
49 | template <class _Tp, class _Up> |
50 | struct __common_type3<_Tp, _Up, void_t<__cond_type<const _Tp&, const _Up&>>> { |
51 | using type _LIBCPP_NODEBUG = remove_cvref_t<__cond_type<const _Tp&, const _Up&>>; |
52 | }; |
53 | |
54 | template <class _Tp, class _Up, class = void> |
55 | struct __common_type2_imp : __common_type3<_Tp, _Up> {}; |
56 | # else |
57 | template <class _Tp, class _Up, class = void> |
58 | struct __common_type2_imp {}; |
59 | # endif |
60 | |
61 | // sub-bullet 3 - "if decay_t<decltype(false ? declval<D1>() : declval<D2>())> ..." |
62 | template <class _Tp, class _Up> |
63 | struct __common_type2_imp<_Tp, _Up, __void_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())> > { |
64 | using type _LIBCPP_NODEBUG = __decay_t<decltype(true ? std::declval<_Tp>() : std::declval<_Up>())>; |
65 | }; |
66 | |
67 | template <class, class = void> |
68 | struct __common_type_impl {}; |
69 | |
70 | template <class... _Tp> |
71 | struct __common_types; |
72 | template <class... _Tp> |
73 | struct common_type; |
74 | |
75 | template <class _Tp, class _Up> |
76 | struct __common_type_impl< __common_types<_Tp, _Up>, __void_t<typename common_type<_Tp, _Up>::type> > { |
77 | typedef typename common_type<_Tp, _Up>::type type; |
78 | }; |
79 | |
80 | template <class _Tp, class _Up, class _Vp, class... _Rest> |
81 | struct __common_type_impl<__common_types<_Tp, _Up, _Vp, _Rest...>, __void_t<typename common_type<_Tp, _Up>::type> > |
82 | : __common_type_impl<__common_types<typename common_type<_Tp, _Up>::type, _Vp, _Rest...> > {}; |
83 | |
84 | // bullet 1 - sizeof...(Tp) == 0 |
85 | |
86 | template <> |
87 | struct common_type<> {}; |
88 | |
89 | // bullet 2 - sizeof...(Tp) == 1 |
90 | |
91 | template <class _Tp> |
92 | struct common_type<_Tp> : public common_type<_Tp, _Tp> {}; |
93 | |
94 | // bullet 3 - sizeof...(Tp) == 2 |
95 | |
96 | // sub-bullet 1 - "If is_same_v<T1, D1> is false or ..." |
97 | template <class _Tp, class _Up> |
98 | struct common_type<_Tp, _Up> |
99 | : __conditional_t<_IsSame<_Tp, __decay_t<_Tp> >::value && _IsSame<_Up, __decay_t<_Up> >::value, |
100 | __common_type2_imp<_Tp, _Up>, |
101 | common_type<__decay_t<_Tp>, __decay_t<_Up> > > {}; |
102 | |
103 | // bullet 4 - sizeof...(Tp) > 2 |
104 | |
105 | template <class _Tp, class _Up, class _Vp, class... _Rest> |
106 | struct common_type<_Tp, _Up, _Vp, _Rest...> : __common_type_impl<__common_types<_Tp, _Up, _Vp, _Rest...> > {}; |
107 | |
108 | #endif |
109 | |
110 | #if _LIBCPP_STD_VER >= 14 |
111 | template <class... _Tp> |
112 | using common_type_t = typename common_type<_Tp...>::type; |
113 | #endif |
114 | |
115 | _LIBCPP_END_NAMESPACE_STD |
116 | |
117 | #endif // _LIBCPP___TYPE_TRAITS_COMMON_TYPE_H |
118 |
Warning: This file is not a C or C++ file. It does not have highlighting.