Fix sorting in Contributors.html
[official-gcc.git] / gcc / testsuite / g++.dg / cpp2a / concepts-class.C
blobb50cb421b3b1e0b9c35f8238108f266875028b26
1 // { dg-do compile { target c++20 } }
3 template<typename T>
4 concept Class = __is_class(T);
6 template<typename T>
7 concept Union = __is_union(T);
9 template<typename T>
10 concept One = sizeof(T) >= 4;
12 template<typename T>
13 concept Two = One<T> && sizeof(T) >= 8;
15 // Basic checks
16 template<typename T> requires true struct ok { };
17 template<typename T> requires false struct err { };
19 ok<int> ok1;
20 err<int> err1; // { dg-error "template constraint failure" }
21 err<int>* err2; // { dg-error "template constraint failure" }
23 // Redeclarations
24 template<typename T>
25   requires Class<T>
26 struct S1;
28 template<Class T> // { dg-error "template parameter | different constraints" }
29 struct S1 { };
31 template<typename T>
32   requires Class<T>
33 struct S2;
35 template<typename T>
36   requires Union<T>
37 struct S2; // { dg-error "redeclaration | different constraints" }
40 // Check non-overlapping specializations
41 template<typename T>
42 struct S3 { static const int value = 0; };
44 template<typename T>
45   requires Class<T>
46 struct S3<T> { static const int value = 1; };
48 template<typename T>
49   requires Union<T>
50 struct S3<T> { static const int value = 2; };
52 struct S { };
53 union U { };
55 static_assert(S3<int>::value == 0, "");
56 static_assert(S3<S>::value == 1, "");
57 static_assert(S3<U>::value == 2, "");
59 // Check ordering of partial specializations
60 template<typename T>
61 struct S4 { static const int value = 0;  };
63 template<typename T>
64   requires One<T>
65 struct S4<T> { static const int value = 1; };
67 template<typename T>
68   requires Two<T>
69 struct S4<T> { static const int value = 2; };
71 struct one_type { char x[4]; };
72 struct two_type { char x[8]; };
74 static_assert(S4<char>::value == 0, "");
75 static_assert(S4<one_type>::value == 1, "");
76 static_assert(S4<two_type>::value == 2, "");
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" }
82 // Constraints are checked even when decls are not instantiatied.
83 S5<one_type>* x4b; // { dg-error "constraint|invalid" }
85 // Deduction guides
86 template <class T>
87 concept IsInt = __is_same_as(T,int);
89 template<typename T>
90 struct A
92   int i;
93   A(...);
96 template<typename I>
97   requires IsInt<I>
98 A(I) -> A<I>;
100 A a(1);
101 A a2(1.0);      // { dg-error "class template argument deduction | no matching function for call" }
104 template<typename T>
105 struct S6
107   template<typename U>
108     requires true
109   struct Inner;
112 template<typename T>
113 template<typename U>
114 struct S6<T>::Inner { }; // { dg-error "does not match" }