[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / type-trait-common-type.cpp
blob7190dcad76f1a3bfd4f0170ff6f9a30cbf79a2b4
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)
5 # error
6 #endif
8 // expected-note@*:* {{template declaration from hidden source: template <template <class ...> class, template <class> class, class, class ...>}}
10 void test() {
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}}
16 struct empty_type {};
18 template <class T>
19 struct type_identity {
20 using type = T;
23 template <class...>
24 struct common_type;
26 template <class... Args>
27 using common_type_t = typename common_type<Args...>::type;
29 void test_vla() {
30 int i = 4;
31 int VLA[i];
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...> {};
41 struct Incomplete;
43 template<>
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>));
81 struct S {};
82 struct T : S {};
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 {};
94 template <>
95 struct common_type<NoCommonType, NoCommonType> {};
97 struct CommonTypeInt {};
99 template <>
100 struct common_type<CommonTypeInt, CommonTypeInt> {
101 using type = int;
104 template <>
105 struct common_type<CommonTypeInt, int> {
106 using type = int;
109 template <>
110 struct common_type<int, CommonTypeInt> {
111 using type = int;
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>));
125 template <class T>
126 struct ConvertibleTo {
127 operator T();
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>));
153 #else
154 static_assert(__is_same(common_type_base<const_ref_convertible, int &>, empty_type));
155 #endif
157 struct WeirdConvertible_1p2_p3 {};
159 struct WeirdConvertible3 {
160 operator WeirdConvertible_1p2_p3();
163 struct WeirdConvertible1p2 {
164 operator WeirdConvertible_1p2_p3();
167 template <>
168 struct common_type<WeirdConvertible3, WeirdConvertible1p2> {
169 using type = WeirdConvertible_1p2_p3;
172 template <>
173 struct common_type<WeirdConvertible1p2, WeirdConvertible3> {
174 using type = WeirdConvertible_1p2_p3;
177 struct WeirdConvertible1 {
178 operator WeirdConvertible1p2();
181 struct WeirdConvertible2 {
182 operator WeirdConvertible1p2();
185 template <>
186 struct common_type<WeirdConvertible1, WeirdConvertible2> {
187 using type = WeirdConvertible1p2;
190 template <>
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
200 operator int();
203 template<>
204 struct common_type<PrivateTypeMember, PrivateTypeMember>
206 private:
207 using type = int;
210 static_assert(__is_same(common_type_base<PrivateTypeMember, PrivateTypeMember, PrivateTypeMember>, empty_type));