1 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++17 -Wno-vla-cxx-extension %s
2 // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify -std=c++20 -Wno-vla-cxx-extension %s
4 #if !__has_builtin(__builtin_common_type)
8 // expected-note@*:* {{template declaration from hidden source: template <template <class ...> class, template <class> class, class, class ...>}}
11 __builtin_common_type
<> a
; // expected-error {{too few template arguments for template '__builtin_common_type'}}
12 __builtin_common_type
<1> b
; // expected-error {{template argument for template template parameter must be a class template or type alias template}}
13 __builtin_common_type
<int, 1> c
; // expected-error {{template argument for template template parameter must be a class template or type alias template}}
19 struct type_identity
{
26 template <class... Args
>
27 using common_type_t
= typename common_type
<Args
...>::type
;
32 __builtin_common_type
<common_type_t
, type_identity
, empty_type
, decltype(VLA
)> d
; // expected-error {{variably modified type 'decltype(VLA)' (aka 'int[i]') cannot be used as a template argument}}
35 template <class... Args
>
36 using common_type_base
= __builtin_common_type
<common_type_t
, type_identity
, empty_type
, Args
...>;
38 template <class... Args
>
39 struct common_type
: common_type_base
<Args
...> {};
44 struct common_type
<Incomplete
, Incomplete
>;
46 static_assert(__is_same(common_type_base
<>, empty_type
));
47 static_assert(__is_same(common_type_base
<Incomplete
>, empty_type
));
48 static_assert(__is_same(common_type_base
<char>, type_identity
<char>));
49 static_assert(__is_same(common_type_base
<int>, type_identity
<int>));
50 static_assert(__is_same(common_type_base
<const int>, type_identity
<int>));
51 static_assert(__is_same(common_type_base
<volatile int>, type_identity
<int>));
52 static_assert(__is_same(common_type_base
<const volatile int>, type_identity
<int>));
53 static_assert(__is_same(common_type_base
<int[]>, type_identity
<int*>));
54 static_assert(__is_same(common_type_base
<const int[]>, type_identity
<const int*>));
55 static_assert(__is_same(common_type_base
<void(&)()>, type_identity
<void(*)()>));
56 static_assert(__is_same(common_type_base
<int[], int[]>, type_identity
<int*>));
58 static_assert(__is_same(common_type_base
<int, int>, type_identity
<int>));
59 static_assert(__is_same(common_type_base
<int, long>, type_identity
<long>));
60 static_assert(__is_same(common_type_base
<long, int>, type_identity
<long>));
61 static_assert(__is_same(common_type_base
<long, long>, type_identity
<long>));
63 static_assert(__is_same(common_type_base
<const int, long>, type_identity
<long>));
64 static_assert(__is_same(common_type_base
<const volatile int, long>, type_identity
<long>));
65 static_assert(__is_same(common_type_base
<int, const long>, type_identity
<long>));
66 static_assert(__is_same(common_type_base
<int, const volatile long>, type_identity
<long>));
68 static_assert(__is_same(common_type_base
<int*, long*>, empty_type
));
70 static_assert(__is_same(common_type_base
<int, long, float>, type_identity
<float>));
71 static_assert(__is_same(common_type_base
<unsigned, char, long>, type_identity
<long>));
72 static_assert(__is_same(common_type_base
<long long, long long, long>, type_identity
<long long>));
74 static_assert(__is_same(common_type_base
<int [[clang::address_space(1)]]>, type_identity
<int [[clang::address_space(1)]]>));
75 static_assert(__is_same(common_type_base
<int [[clang::address_space(1)]], int>, type_identity
<int>));
76 static_assert(__is_same(common_type_base
<long [[clang::address_space(1)]], int>, type_identity
<long>));
77 static_assert(__is_same(common_type_base
<long [[clang::address_space(1)]], int [[clang::address_space(1)]]>, type_identity
<long>));
78 static_assert(__is_same(common_type_base
<long [[clang::address_space(1)]], long [[clang::address_space(1)]]>, type_identity
<long [[clang::address_space(1)]]>));
79 static_assert(__is_same(common_type_base
<long [[clang::address_space(1)]], long [[clang::address_space(2)]]>, type_identity
<long>));
84 static_assert(__is_same(common_type_base
<int S::*, int S::*>, type_identity
<int S::*>));
85 static_assert(__is_same(common_type_base
<int S::*, int T::*>, type_identity
<int T::*>));
86 static_assert(__is_same(common_type_base
<int S::*, long S::*>, empty_type
));
88 static_assert(__is_same(common_type_base
<int (S::*)(), int (S::*)()>, type_identity
<int (S::*)()>));
89 static_assert(__is_same(common_type_base
<int (S::*)(), int (T::*)()>, type_identity
<int (T::*)()>));
90 static_assert(__is_same(common_type_base
<int (S::*)(), long (S::*)()>, empty_type
));
92 struct NoCommonType
{};
95 struct common_type
<NoCommonType
, NoCommonType
> {};
97 struct CommonTypeInt
{};
100 struct common_type
<CommonTypeInt
, CommonTypeInt
> {
105 struct common_type
<CommonTypeInt
, int> {
110 struct common_type
<int, CommonTypeInt
> {
114 static_assert(__is_same(common_type_base
<NoCommonType
>, empty_type
));
115 static_assert(__is_same(common_type_base
<CommonTypeInt
>, type_identity
<int>));
116 static_assert(__is_same(common_type_base
<NoCommonType
, NoCommonType
, NoCommonType
>, empty_type
));
117 static_assert(__is_same(common_type_base
<CommonTypeInt
, CommonTypeInt
, CommonTypeInt
>, type_identity
<int>));
118 static_assert(__is_same(common_type_base
<CommonTypeInt
&, CommonTypeInt
&&>, type_identity
<int>));
120 static_assert(__is_same(common_type_base
<void, int>, empty_type
));
121 static_assert(__is_same(common_type_base
<void, void>, type_identity
<void>));
122 static_assert(__is_same(common_type_base
<const void, void>, type_identity
<void>));
123 static_assert(__is_same(common_type_base
<void, const void>, type_identity
<void>));
126 struct ConvertibleTo
{
130 static_assert(__is_same(common_type_base
<ConvertibleTo
<int>>, type_identity
<ConvertibleTo
<int>>));
131 static_assert(__is_same(common_type_base
<ConvertibleTo
<int>, int>, type_identity
<int>));
132 static_assert(__is_same(common_type_base
<ConvertibleTo
<int&>, ConvertibleTo
<long&>>, type_identity
<long>));
134 struct ConvertibleToB
;
136 struct ConvertibleToA
{
137 operator ConvertibleToB();
140 struct ConvertibleToB
{
141 operator ConvertibleToA();
144 static_assert(__is_same(common_type_base
<ConvertibleToA
, ConvertibleToB
>, empty_type
));
146 struct const_ref_convertible
{
147 operator int&() const &;
148 operator int&() && = delete;
151 #if __cplusplus >= 202002L
152 static_assert(__is_same(common_type_base
<const_ref_convertible
, int &>, type_identity
<int>));
154 static_assert(__is_same(common_type_base
<const_ref_convertible
, int &>, empty_type
));
157 struct WeirdConvertible_1p2_p3
{};
159 struct WeirdConvertible3
{
160 operator WeirdConvertible_1p2_p3();
163 struct WeirdConvertible1p2
{
164 operator WeirdConvertible_1p2_p3();
168 struct common_type
<WeirdConvertible3
, WeirdConvertible1p2
> {
169 using type
= WeirdConvertible_1p2_p3
;
173 struct common_type
<WeirdConvertible1p2
, WeirdConvertible3
> {
174 using type
= WeirdConvertible_1p2_p3
;
177 struct WeirdConvertible1
{
178 operator WeirdConvertible1p2();
181 struct WeirdConvertible2
{
182 operator WeirdConvertible1p2();
186 struct common_type
<WeirdConvertible1
, WeirdConvertible2
> {
187 using type
= WeirdConvertible1p2
;
191 struct common_type
<WeirdConvertible2
, WeirdConvertible1
> {
192 using type
= WeirdConvertible1p2
;
195 static_assert(__is_same(common_type_base
<WeirdConvertible1
, WeirdConvertible2
, WeirdConvertible3
>,
196 type_identity
<WeirdConvertible_1p2_p3
>));
198 struct PrivateTypeMember
204 struct common_type
<PrivateTypeMember
, PrivateTypeMember
>
210 static_assert(__is_same(common_type_base
<PrivateTypeMember
, PrivateTypeMember
, PrivateTypeMember
>, empty_type
));