1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
// RUN: %clang_cc1 -std=c++20 -verify %s
// expected-no-diagnostics
static constexpr int PRIMARY = 0;
static constexpr int SPECIALIZATION_CONCEPT = 1;
static constexpr int SPECIALIZATION_REQUIRES = 2;
template <class T>
concept Concept = (sizeof(T) >= 2 * sizeof(int));
struct XY {
int x;
int y;
};
namespace members {
template <class T, class U> struct S {
static constexpr int primary();
};
template <class T, class U> constexpr int S<T, U>::primary() {
return PRIMARY;
};
template <Concept C, class U> struct S<C, U> {
static constexpr int specialization();
};
template <class T, class U>
requires(sizeof(T) == sizeof(int))
struct S<T, U> {
static constexpr int specialization();
};
template <Concept C, class U> constexpr int S<C, U>::specialization() {
return SPECIALIZATION_CONCEPT;
}
template <class T, class U>
requires(sizeof(T) == sizeof(int))
constexpr int S<T, U>::specialization() {
return SPECIALIZATION_REQUIRES;
}
static_assert(S<char, double>::primary() == PRIMARY);
static_assert(S<XY, double>::specialization() == SPECIALIZATION_CONCEPT);
static_assert(S<int, double>::specialization() == SPECIALIZATION_REQUIRES);
} // namespace members
namespace enumerations {
template <class T, class U> struct S {
enum class E : int;
};
template <class T, class U> enum class S<T, U>::E { Value = PRIMARY };
template <Concept C, class U> struct S<C, U> {
enum class E : int;
};
template <Concept C, class U>
enum class S<C, U>::E {
Value = SPECIALIZATION_CONCEPT
};
template <class T, class U>
requires(sizeof(T) == sizeof(int))
struct S<T, U> {
enum class E : int;
};
template <class T, class U>
requires(sizeof(T) == sizeof(int))
enum class S<T, U>::E {
Value = SPECIALIZATION_REQUIRES
};
static_assert(static_cast<int>(S<char, double>::E::Value) == PRIMARY);
static_assert(static_cast<int>(S<XY, double>::E::Value) ==
SPECIALIZATION_CONCEPT);
static_assert(static_cast<int>(S<int, double>::E::Value) ==
SPECIALIZATION_REQUIRES);
} // namespace enumerations
namespace multiple_template_parameter_lists {
template <class Outer>
struct S {
template <class Inner>
static constexpr int primary(Inner);
};
template <class Outer>
template <class Inner>
constexpr int S<Outer>::primary(Inner) {
return PRIMARY;
};
template <Concept Outer>
struct S<Outer> {
template <class Inner>
static constexpr int specialization(Inner);
};
template <Concept Outer>
template <class Inner>
constexpr int S<Outer>::specialization(Inner) { return SPECIALIZATION_CONCEPT; }
template <class Outer>
requires(sizeof(Outer) == sizeof(int))
struct S<Outer> {
template <class Inner>
static constexpr int specialization(Inner);
};
template <class Outer>
requires(sizeof(Outer) == sizeof(int))
template <class Inner>
constexpr int S<Outer>::specialization(Inner) { return SPECIALIZATION_REQUIRES; }
static_assert(S<char>::primary("str") == PRIMARY);
static_assert(S<XY>::specialization("str") == SPECIALIZATION_CONCEPT);
static_assert(S<int>::specialization("str") == SPECIALIZATION_REQUIRES);
} // namespace multiple_template_parameter_lists
static constexpr int CONSTRAINED_METHOD_1 = 1;
static constexpr int CONSTRAINED_METHOD_2 = 2;
namespace constrained_members {
template <int>
struct S {
template <Concept C>
static constexpr int constrained_method();
};
template <>
template <Concept C>
constexpr int S<1>::constrained_method() { return CONSTRAINED_METHOD_1; }
template <>
template <Concept C>
constexpr int S<2>::constrained_method() { return CONSTRAINED_METHOD_2; }
static_assert(S<1>::constrained_method<XY>() == CONSTRAINED_METHOD_1);
static_assert(S<2>::constrained_method<XY>() == CONSTRAINED_METHOD_2);
template <class T1, class T2>
concept ConceptT1T2 = true;
template<typename T3>
struct S12 {
template<ConceptT1T2<T3> T4>
static constexpr int constrained_method();
};
template<>
template<ConceptT1T2<int> T5>
constexpr int S12<int>::constrained_method() { return CONSTRAINED_METHOD_1; }
template<>
template<ConceptT1T2<double> T5>
constexpr int S12<double>::constrained_method() { return CONSTRAINED_METHOD_2; }
static_assert(S12<int>::constrained_method<XY>() == CONSTRAINED_METHOD_1);
static_assert(S12<double>::constrained_method<XY>() == CONSTRAINED_METHOD_2);
} // namespace constrained members
namespace constrained_members_of_nested_types {
template <int>
struct S {
struct Inner0 {
struct Inner1 {
template <Concept C>
static constexpr int constrained_method();
};
};
};
template <>
template <Concept C>
constexpr int S<1>::Inner0::Inner1::constrained_method() { return CONSTRAINED_METHOD_1; }
template <>
template <Concept C>
constexpr int S<2>::Inner0::Inner1::constrained_method() { return CONSTRAINED_METHOD_2; }
static_assert(S<1>::Inner0::Inner1::constrained_method<XY>() == CONSTRAINED_METHOD_1);
static_assert(S<2>::Inner0::Inner1::constrained_method<XY>() == CONSTRAINED_METHOD_2);
template <class T1, class T2>
concept ConceptT1T2 = true;
template<typename T3>
struct S12 {
struct Inner0 {
struct Inner1 {
template<ConceptT1T2<T3> T4>
static constexpr int constrained_method();
};
};
};
template<>
template<ConceptT1T2<int> T5>
constexpr int S12<int>::Inner0::Inner1::constrained_method() { return CONSTRAINED_METHOD_1; }
template<>
template<ConceptT1T2<double> T5>
constexpr int S12<double>::Inner0::Inner1::constrained_method() { return CONSTRAINED_METHOD_2; }
static_assert(S12<int>::Inner0::Inner1::constrained_method<XY>() == CONSTRAINED_METHOD_1);
static_assert(S12<double>::Inner0::Inner1::constrained_method<XY>() == CONSTRAINED_METHOD_2);
} // namespace constrained_members_of_nested_types
namespace constrained_member_sfinae {
template<int N> struct S {
template<class T>
static constexpr int constrained_method() requires (sizeof(int[N * 1073741824 + 4]) == 16) {
return CONSTRAINED_METHOD_1;
}
template<class T>
static constexpr int constrained_method() requires (sizeof(int[N]) == 16);
};
template<>
template<typename T>
constexpr int S<4>::constrained_method() requires (sizeof(int[4]) == 16) {
return CONSTRAINED_METHOD_2;
}
// Verify that there is no amiguity in this case.
static_assert(S<4>::constrained_method<double>() == CONSTRAINED_METHOD_2);
} // namespace constrained_member_sfinae
namespace requires_expression_references_members {
void accept1(int x);
void accept2(XY xy);
template <class T> struct S {
T Field = T();
constexpr int constrained_method()
requires requires { accept1(Field); };
constexpr int constrained_method()
requires requires { accept2(Field); };
};
template <class T>
constexpr int S<T>::constrained_method()
requires requires { accept1(Field); } {
return CONSTRAINED_METHOD_1;
}
template <class T>
constexpr int S<T>::constrained_method()
requires requires { accept2(Field); } {
return CONSTRAINED_METHOD_2;
}
static_assert(S<int>().constrained_method() == CONSTRAINED_METHOD_1);
static_assert(S<XY>().constrained_method() == CONSTRAINED_METHOD_2);
} // namespace requires_expression_references_members
namespace GH60231 {
template<typename T0> concept C = true;
template <typename T1>
struct S {
template <typename F1> requires C<S<T1>>
void foo1(F1 f);
template <typename F2>
void foo2(F2 f) requires C<S<T1>>;
template <typename F3> requires C<F3>
void foo3(F3 f);
};
template <typename T2>
template <typename F4> requires C<S<T2>>
void S<T2>::foo1(F4 f) {}
template <typename T3>
template <typename F5>
void S<T3>::foo2(F5 f) requires C<S<T3>> {}
template <typename T4>
template <typename F6> requires C<F6>
void S<T4>::foo3(F6 f) {}
} // namespace GH60231
namespace GH62003 {
template <typename T0> concept Concept = true;
template <class T1>
struct S1 {
template <Concept C1>
static constexpr int foo();
};
template <class T2>
template <Concept C2>
constexpr int S1<T2>::foo() { return 1; }
template <Concept C3>
struct S2 {
template <class T3>
static constexpr int foo();
};
template <Concept C4>
template <class T4>
constexpr int S2<C4>::foo() { return 2; }
template <Concept C5>
struct S3 {
template <Concept C6>
static constexpr int foo();
};
template <Concept C7>
template <Concept C8>
constexpr int S3<C7>::foo() { return 3; }
static_assert(S1<int>::foo<int>() == 1);
static_assert(S2<int>::foo<int>() == 2);
static_assert(S3<int>::foo<int>() == 3);
} // namespace GH62003
namespace MultilevelTemplateWithPartialSpecialization {
template <typename>
concept Concept = true;
namespace two_level {
template <typename T1, int>
struct W0 {
template <typename T2>
requires (Concept<T2>)
void f(const T2 &);
};
template <typename T3>
struct W0<T3, 0> {
template <typename T4>
requires (Concept<T4>)
void f(const T4 &);
};
template <typename T3>
template <typename T4>
requires (Concept<T4>)
inline void W0<T3, 0>::f(const T4 &) {}
} // namespace two_level
namespace three_level {
template <typename T1, int>
struct W0 {
template <typename T2>
struct W1 {
template <typename T3>
requires (Concept<T3>)
void f(const T3 &);
};
};
template <typename T4>
struct W0<T4, 0> {
template <typename T5>
struct W1 {
template <typename T6>
requires (Concept<T6>)
void f(const T6 &);
};
};
template <typename T7>
template <typename T8>
template <typename T9>
requires (Concept<T9>)
inline void W0<T7, 0>::W1<T8>::f(const T9 &) {}
} // namespace three_level
} // namespace MultilevelTemplateWithPartialSpecialization
|