Debian Codegraph @

clang/test/SemaCXX/cxx2c-fold-exprs.cpp

raw
1 // RUN: %clang_cc1 -std=c++2c -verify %s
2
3 template <class T> concept A = true;
4 template <class T> concept C = A<T> && true;
5 template <class T> concept D = A<T> && __is_same(T, int);
6
7
8 template <class T> requires (A<T>)
9 constexpr int f(T) { return 0; };
10 template <class... T> requires (C<T> && ...)
11 constexpr int f(T...) { return 1; };
12
13 static_assert(f(0) == 0);
14 static_assert(f(1) == 0);
15
16
17 template <class... T> requires (A<T> && ...)
18 constexpr int g(T...) { return 0; };
19 template <class... T> requires (C<T> && ...)
20 constexpr int g(T...) { return 1; };
21
22 static_assert(g(0) == 1);
23 static_assert(g() == 1);
24 static_assert(g(1, 2) == 1);
25
26
27
28 template <class... T> requires (A<T> && ...)
29 constexpr int h(T...) { return 0; }; // expected-note {{candidate}}
30 template <class... T> requires (C<T> || ...)
31 constexpr int h(T...) { return 1; }; // expected-note {{candidate}}
32
33 static_assert(h(0) == 1); // expected-error {{call to 'h' is ambiguous}}
34
35 template <class... T> requires (A<T> || ...)
36 constexpr int i(T...) { return 0; }; // expected-note {{candidate}}
37 template <class... T> requires (C<T> && ...)
38 constexpr int i(T...) { return 1; }; // expected-note {{candidate}}
39
40 static_assert(i(0) == 1); // expected-error {{call to 'i' is ambiguous}}
41
42
43 template <class... T> requires (A<T> || ... || true)
44 constexpr int j(T...) { return 0; };
45 template <class... T> requires (C<T> && ... && true)
46 constexpr int j(T...) { return 1; };
47
48 static_assert(j(0) == 1);
49 static_assert(j() == 1);
50
51
52
53 template <class... T> requires (A<T> || ...)
54 constexpr int k(T...) { return 0; }; // expected-note {{candidate template ignored: constraints not satisfied [with T = <>]}}
55 template <class... T> requires (C<T> || ...)
56 constexpr int k(T...) { return 1; }; // expected-note {{candidate template ignored: constraints not satisfied [with T = <>]}}
57
58 static_assert(k(0) == 1);
59 static_assert(k() == 0); // expected-error {{no matching function for call to 'k'}}
60 static_assert(k(1, 2) == 1);
61
62
63 consteval int terse(A auto...) {return 1;}
64 consteval int terse(D auto...) {return 2;}
65
66 static_assert(terse() == 2);
67 static_assert(terse(0, 0) == 2);
68 static_assert(terse(0L, 0) == 1);
69
70 template <A... T>
71 consteval int tpl_head(A auto...) {return 1;}
72 template <D... T>
73 consteval int tpl_head(D auto...) {return 2;}
74
75 static_assert(tpl_head() == 2);
76 static_assert(tpl_head(0, 0) == 2);
77 static_assert(tpl_head(0L, 0) == 1);
78
79
80 namespace equivalence {
81
82 template <typename... T>
83 struct S {
84 template <typename... U>
85 void f() requires (A<U> && ...);
86 template <typename... U>
87 void f() requires (C<U> && ...);
88
89 template <typename... U>
90 void g() requires (A<T> && ...);
91 template <typename... U>
92 void g() requires (C<T> && ...);
93
94 template <typename... U>
95 void h() requires (A<U> && ...); // expected-note {{candidate}}
96 template <typename... U>
97 void h() requires (C<T> && ...); // expected-note {{candidate}}
98 };
99
100 void test() {
101 S<int>{}.f<int>();
102 S<int>{}.g<int>();
103 S<int>{}.h<int>(); // expected-error {{call to member function 'h' is ambiguous}}
104 }
105
106
107 }
108
109 namespace substitution {
110 struct S {
111 using type = int;
112 };
113
114 template <typename... T>
115 consteval int And1() requires (C<typename T::type> && ...) { // #and1
116 return 1;
117 }
118
119 template <typename T, typename... U>
120 consteval int And2() requires (C<typename U::type> && ... && C<typename T::type>) { // #and2
121 return 2;
122 }
123
124 template <typename T, typename... U>
125 consteval int And3() requires (C<typename T::type> && ... && C<typename U::type>) { // #and3
126 return 3;
127 }
128
129 template <typename... T>
130 consteval int Or1() requires (C<typename T::type> || ...) { // #or1
131 return 1;
132 }
133
134 template <typename T, typename... U>
135 consteval int Or2() requires (C<typename U::type> || ... || C<typename T::type>) { // #or2
136 return 2;
137 }
138
139 template <typename T, typename... U>
140 consteval int Or3() requires (C<typename T::type> || ... || C<typename U::type>) { // #or3
141 return 3;
142 }
143
144 static_assert(And1<>() == 1);
145 static_assert(And1<S>() == 1);
146 static_assert(And1<S, S>() == 1);
147 static_assert(And1<int>() == 1); // expected-error {{no matching function for call to 'And1'}}
148 // expected-note@#and1 {{candidate template ignored: constraints not satisfied}}
149 // expected-note@#and1 {{because substituted constraint expression is ill-formed}}
150
151 static_assert(And1<S, int>() == 1); // expected-error {{no matching function for call to 'And1'}}
152 // expected-note@#and1 {{candidate template ignored: constraints not satisfied}}
153 // expected-note@#and1 {{because substituted constraint expression is ill-formed}}
154
155 static_assert(And1<int, S>() == 1); // expected-error {{no matching function for call to 'And1'}}
156 // expected-note@#and1 {{candidate template ignored: constraints not satisfied}}
157 // expected-note@#and1 {{because substituted constraint expression is ill-formed}}
158
159 static_assert(And2<S>() == 2);
160 static_assert(And2<S, S>() == 2);
161 static_assert(And2<int>() == 2);
162
163 static_assert(And2<int, int>() == 2); // expected-error {{no matching function for call to 'And2'}}
164 // expected-note@#and2 {{candidate template ignored: constraints not satisfied}}
165 // expected-note@#and2 {{because substituted constraint expression is ill-formed}}
166
167 static_assert(And2<S, int>() == 2); // expected-error {{no matching function for call to 'And2'}}
168 // expected-note@#and2 {{candidate template ignored: constraints not satisfied}}
169 // expected-note@#and2 {{because substituted constraint expression is ill-formed}}
170
171 static_assert(And2<int, S>() == 2); // expected-error {{no matching function for call to 'And2'}}
172 // expected-note@#and2 {{candidate template ignored: constraints not satisfied}}
173 // expected-note@#and2 {{because substituted constraint expression is ill-formed}}
174
175 static_assert(And3<S>() == 3);
176 static_assert(And3<S, S>() == 3);
177 static_assert(And3<int>() == 3); // expected-error {{no matching function for call to 'And3'}}
178 // expected-note@#and3 {{candidate template ignored: constraints not satisfied}}
179 // expected-note@#and3 {{because substituted constraint expression is ill-formed}}
180
181 static_assert(And3<int, int>() == 3); // expected-error {{no matching function for call to 'And3'}}
182 // expected-note@#and3 {{candidate template ignored: constraints not satisfied}}
183 // expected-note@#and3 {{because substituted constraint expression is ill-formed}}
184
185 static_assert(And3<S, int>() == 3); // expected-error {{no matching function for call to 'And3'}}
186 // expected-note@#and3 {{candidate template ignored: constraints not satisfied}}
187 // expected-note@#and3 {{because substituted constraint expression is ill-formed}}
188
189 static_assert(And3<int, S>() == 3); // expected-error {{no matching function for call to 'And3'}}
190 // expected-note@#and3 {{candidate template ignored: constraints not satisfied}}
191 // expected-note@#and3 {{because substituted constraint expression is ill-formed}}
192
193
194 static_assert(Or1<>() == 1); // expected-error {{no matching function for call to 'Or1'}}
195 // expected-note@#or1 {{candidate template ignored: constraints not satisfied}}
196 static_assert(Or1<S>() == 1);
197 static_assert(Or1<int, S>() == 1);
198 static_assert(Or1<S, int>() == 1);
199 static_assert(Or1<S, S>() == 1);
200 static_assert(Or1<int>() == 1); // expected-error {{no matching function for call to 'Or1'}}
201 // expected-note@#or1 {{candidate template ignored: constraints not satisfied}} \
202 // expected-note@#or1 {{because substituted constraint expression is ill-formed}}
203
204
205 static_assert(Or2<S>() == 2);
206 static_assert(Or2<int, S>() == 2);
207 static_assert(Or2<S, int>() == 2);
208 static_assert(Or2<S, S>() == 2);
209 static_assert(Or2<int>() == 2); // expected-error {{no matching function for call to 'Or2'}}
210 // expected-note@#or2 {{candidate template ignored: constraints not satisfied}} \
211 // expected-note@#or2 {{because substituted constraint expression is ill-formed}}
212
213 static_assert(Or3<S>() == 3);
214 static_assert(Or3<int, S>() == 3);
215 static_assert(Or3<S, int>() == 3);
216 static_assert(Or3<S, S>() == 3);
217 static_assert(Or3<int>() == 3); // expected-error {{no matching function for call to 'Or3'}}
218 // expected-note@#or3 {{candidate template ignored: constraints not satisfied}} \
219 // expected-note@#or3 {{because substituted constraint expression is ill-formed}}
220 }
221
222 namespace bool_conversion_break {
223
224 template <typename ...V> struct A;
225 struct Thingy {
226 static constexpr int compare(const Thingy&) {return 1;}
227 };
228 template <typename ...T, typename ...U>
229 void f(A<T ...> *, A<U ...> *) // expected-note {{candidate template ignored: failed template argument deduction}}
230 requires (T::compare(U{}) && ...); // expected-error {{atomic constraint must be of type 'bool' (found 'int')}}
231
232 void g() {
233 A<Thingy, Thingy> *ap;
234 f(ap, ap); // expected-error{{no matching function for call to 'f'}} \
235 // expected-note {{while checking constraint satisfaction}} \
236 // expected-note {{in instantiation of function template specialization}}
237 }
238
239 }
240
241 namespace nested {
242
243 template <typename... T>
244 struct S {
245 template <typename... U>
246 consteval static int f()
247 requires ((A<T> && ...) && ... && A<U> ) {
248 return 1;
249 }
250
251 template <typename... U>
252 consteval static int f()
253 requires ((C<T> && ...) && ... && C<U> ) {
254 return 2;
255 }
256
257 template <typename... U>
258 consteval static int g() // #nested-ambiguous-g1
259 requires ((A<T> && ...) && ... && A<U> ) {
260 return 1;
261 }
262
263 template <typename... U>
264 consteval static int g() // #nested-ambiguous-g2
265 requires ((C<U> && ...) && ... && C<T> ) {
266 return 2;
267 }
268 };
269
270 static_assert(S<int>::f<int>() == 2);
271
272 static_assert(S<int>::g<int>() == 2); // expected-error {{call to 'g' is ambiguous}}
273 // expected-note@#nested-ambiguous-g1 {{candidate}}
274 // expected-note@#nested-ambiguous-g2 {{candidate}}
275
276
277 }
278
279 namespace GH99430 {
280
281 template <class _Ty1, class _Ty2>
282 using _Synth_three_way_result = int;
283
284 template <class... _Types>
285 class tuple;
286
287 template <int _Index>
288 struct tuple_element;
289
290 template <class, int...>
291 struct _Three_way_comparison_result_with_tuple_like {
292 using type = int;
293 };
294
295 template <class... _TTypes, int... _Indices>
296 requires(requires {
297 typename _Synth_three_way_result<_TTypes, tuple_element<_Indices>>;
298 } && ...)
299
300 struct _Three_way_comparison_result_with_tuple_like<tuple<_TTypes...>, _Indices...>{
301 using type = long;
302 };
303
304 static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<int>, 0, 1>::type, int));
305 static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<int>, 0>::type, long));
306
307 }