Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/concepts-requires18.C

raw
1 // { dg-do compile { target c++20 } }
2
3 template<typename T>
4 concept integer = __is_same_as(T, int);
5
6 template<typename T>
7 concept subst = requires (T x) { requires true; };
8
9 template<typename T>
10 concept c1 = requires { requires integer<T> || subst<T&>; }; // { dg-message "in requirements" }
11
12 static_assert(requires { requires true; });
13 static_assert(requires { requires false; }); // { dg-error "static assertion failed" }
14 static_assert(requires { requires integer<int>; });
15 static_assert(requires { requires integer<void>; }); // { dg-error "static assertion failed" }
16 static_assert(requires { requires c1<int>; });
17 static_assert(requires { requires c1<bool>; });
18 static_assert(requires { requires c1<void>; }); // { dg-error "static assertion failed" }
19 static_assert(requires { requires subst<void&>; }); // { dg-error "cannot declare|failed" }
20
21 static_assert(c1<int>);
22 static_assert(c1<bool>);
23 static_assert(c1<void>); // { dg-error "static assertion failed" }
24
25 template<c1 T>
26 void f1() { }
27
28 template<typename T>
29 requires requires { requires integer<T> || subst<T&>; } // { dg-message "in requirements" }
30 void f2();
31
32 template<typename T>
33 struct data
34 {
35 template<c1 U>
36 void f1() {}
37
38 template<typename U>
39 requires requires { requires integer<U> || subst<U&>; } // { dg-message in requirements" }
40 void f2() {}
41
42 static_assert(requires { requires subst<T&>; }); // { dg-error "forming reference|failed" }
43
44 template<typename U>
45 constexpr bool test()
46 {
47 if constexpr (requires { requires subst<U&>; })
48 return true;
49 else
50 return false;
51 }
52 };
53
54 template<typename T>
55 constexpr bool check_for_resize(T &v, unsigned const n)
56 {
57 if constexpr (requires { v.resize(n); })
58 return true;
59 else
60 return false;
61 }
62
63 struct array { };
64 struct vector { void resize(int n); };
65
66 void test()
67 {
68 f1<int>();
69 f1<bool>();
70 f1<void>(); // { dg-error "no match" }
71
72 f2<int>();
73 f2<bool>();
74 f2<void>(); // { dg-error "no match" }
75
76 data<char> x;
77 x.f1<int>();
78 x.f1<bool>();
79 x.f1<void>(); // { dg-error "no matching function" }
80 x.f2<int>();
81 x.f2<bool>();
82 x.f2<void>(); // { dg-error "no matching function" }
83
84 data<void> fail;
85
86 data<int> t;
87 static_assert(t.test<int>());
88 static_assert(t.test<void>()); // { dg-error "static assertion failed" }
89
90 vector v;
91 static_assert(check_for_resize(v, 10));
92 array a;
93 static_assert(!check_for_resize(a, 10));
94 }