| 1 |
// { dg-do compile { target c++20 } } |
| 2 |
|
| 3 |
template<typename T> |
| 4 |
concept Class = __is_class(T); |
| 5 |
|
| 6 |
template<typename T> |
| 7 |
concept Union = __is_union(T); |
| 8 |
|
| 9 |
template<typename T> |
| 10 |
concept One = sizeof(T) >= 4; |
| 11 |
|
| 12 |
template<typename T> |
| 13 |
concept Two = One<T> && sizeof(T) >= 8; |
| 14 |
|
| 15 |
// Basic checks |
| 16 |
template<typename T> requires true struct ok { }; |
| 17 |
template<typename T> requires false struct err { }; |
| 18 |
|
| 19 |
ok<int> ok1; |
| 20 |
err<int> err1; // { dg-error "template constraint failure" } |
| 21 |
err<int>* err2; // { dg-error "template constraint failure" } |
| 22 |
|
| 23 |
// Redeclarations |
| 24 |
template<typename T> |
| 25 |
requires Class<T> |
| 26 |
struct S1; |
| 27 |
|
| 28 |
template<Class T> // { dg-error "template parameter | different constraints" } |
| 29 |
struct S1 { }; |
| 30 |
|
| 31 |
template<typename T> |
| 32 |
requires Class<T> |
| 33 |
struct S2; |
| 34 |
|
| 35 |
template<typename T> |
| 36 |
requires Union<T> |
| 37 |
struct S2; // { dg-error "redeclaration | different constraints" } |
| 38 |
|
| 39 |
|
| 40 |
// Check non-overlapping specializations |
| 41 |
template<typename T> |
| 42 |
struct S3 { static const int value = 0; }; |
| 43 |
|
| 44 |
template<typename T> |
| 45 |
requires Class<T> |
| 46 |
struct S3<T> { static const int value = 1; }; |
| 47 |
|
| 48 |
template<typename T> |
| 49 |
requires Union<T> |
| 50 |
struct S3<T> { static const int value = 2; }; |
| 51 |
|
| 52 |
struct S { }; |
| 53 |
union U { }; |
| 54 |
|
| 55 |
static_assert(S3<int>::value == 0, ""); |
| 56 |
static_assert(S3<S>::value == 1, ""); |
| 57 |
static_assert(S3<U>::value == 2, ""); |
| 58 |
|
| 59 |
// Check ordering of partial specializations |
| 60 |
template<typename T> |
| 61 |
struct S4 { static const int value = 0; }; |
| 62 |
|
| 63 |
template<typename T> |
| 64 |
requires One<T> |
| 65 |
struct S4<T> { static const int value = 1; }; |
| 66 |
|
| 67 |
template<typename T> |
| 68 |
requires Two<T> |
| 69 |
struct S4<T> { static const int value = 2; }; |
| 70 |
|
| 71 |
struct one_type { char x[4]; }; |
| 72 |
struct two_type { char x[8]; }; |
| 73 |
|
| 74 |
static_assert(S4<char>::value == 0, ""); |
| 75 |
static_assert(S4<one_type>::value == 1, ""); |
| 76 |
static_assert(S4<two_type>::value == 2, ""); |
| 77 |
|
| 78 |
// Specializations are more specialized. |
| 79 |
template<typename T> requires Two<T> struct S5 { }; |
| 80 |
template<typename T> requires One<T> struct S5<T> { }; // { dg-error "does not specialize" } |
| 81 |
|
| 82 |
// Constraints are checked even when decls are not instantiatied. |
| 83 |
S5<one_type>* x4b; // { dg-error "constraint|invalid" } |
| 84 |
|
| 85 |
// Deduction guides |
| 86 |
template <class T> |
| 87 |
concept IsInt = __is_same_as(T,int); |
| 88 |
|
| 89 |
template<typename T> |
| 90 |
struct A |
| 91 |
{ |
| 92 |
int i; |
| 93 |
A(...); |
| 94 |
}; |
| 95 |
|
| 96 |
template<typename I> |
| 97 |
requires IsInt<I> |
| 98 |
A(I) -> A<I>; |
| 99 |
|
| 100 |
A a(1); |
| 101 |
A a2(1.0); // { dg-error "class template argument deduction | no matching function for call" } |
| 102 |
|
| 103 |
|
| 104 |
template<typename T> |
| 105 |
struct S6 |
| 106 |
{ |
| 107 |
template<typename U> |
| 108 |
requires true |
| 109 |
struct Inner; |
| 110 |
}; |
| 111 |
|
| 112 |
template<typename T> |
| 113 |
template<typename U> |
| 114 |
struct S6<T>::Inner { }; // { dg-error "does not match" } |
| 115 |
|