cxx-concepts-requires-clause.cpp
4.22 KB
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
// RUN: %clang_cc1 -std=c++2a -x c++ %s -verify
// Test parsing of the optional requires-clause in a template-declaration.
template <typename T> requires true
void foo() { }
template <typename T> requires (!0)
struct A {
void foo();
struct AA;
enum E : int;
static int x;
template <typename> requires true
void Mfoo();
template <typename> requires true
struct M;
template <typename> requires true
static int Mx;
template <typename TT> requires true
using MQ = M<TT>;
};
template <typename T> requires (!0)
void A<T>::foo() { }
template <typename T> requires (!0)
struct A<T>::AA { };
template <typename T> requires (!0)
enum A<T>::E : int { E0 };
template <typename T> requires (!0)
int A<T>::x = 0;
template <typename T> requires (!0)
template <typename> requires true
void A<T>::Mfoo() { }
template <typename T> requires (!0)
template <typename> requires true
struct A<T>::M { };
template <typename T> requires (!0)
template <typename> requires true
int A<T>::Mx = 0;
template <typename T> requires true
int x = 0;
template <typename T> requires true
using Q = A<T>;
struct C {
template <typename> requires true
void Mfoo();
template <typename> requires true
struct M;
template <typename> requires true
static int Mx;
template <typename T> requires true
using MQ = M<T>;
};
template <typename> requires true
void C::Mfoo() { }
template <typename> requires true
struct C::M { };
template <typename> requires true
int C::Mx = 0;
// Test behavior with non-primary-expression requires clauses
template<typename T> requires foo<T>()
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
struct B1 { };
int func() { }
template<typename T> requires func()
// expected-error@-1{{atomic constraint must be of type 'bool' (found '<overloaded function type>')}}
// expected-note@-2{{parentheses are required around this expression in a requires clause}}
struct B2 { };
template<typename T> requires (foo<T>())
struct B3 { };
template<typename T> requires T{}
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
struct B4 { };
template<typename T> requires sizeof(T) == 0
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
struct B5 { };
template<typename T> requires (sizeof(T)) == 0
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
struct B6 { };
template<typename T> requires 0
// expected-error@-1{{atomic constraint must be of type 'bool' (found 'int')}}
(int) bar() { };
template<typename T> requires foo<T>
(int) bar() { };
// expected-error@-1{{expected '(' for function-style cast or type construction}}
template<typename T>
void bar() requires foo<T>();
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
template<typename T>
void bar() requires (foo<T>());
template<typename T>
void bar() requires func();
// expected-error@-1{{atomic constraint must be of type 'bool' (found '<overloaded function type>')}}
// expected-note@-2{{parentheses are required around this expression in a requires clause}}
template<typename T>
void bar() requires T{};
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
template<typename T>
void bar() requires sizeof(T) == 0;
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
template<typename T>
void bar() requires (sizeof(T)) == 0;
// expected-error@-1{{parentheses are required around this expression in a requires clause}}
void bar(int x, int y) requires (x, y, true);
struct B {
int x;
void foo(int y) requires (x, this, this->x, y, true);
static void bar(int y) requires (x, true);
// expected-error@-1{{'this' cannot be implicitly used in a static member function declaration}}
static void baz(int y) requires (this, true);
// expected-error@-1{{'this' cannot be used in a static member function declaration}}
};
auto lambda1 = [] (auto x) requires (sizeof(decltype(x)) == 1) { };
auto lambda2 = [] (auto x) constexpr -> int requires (sizeof(decltype(x)) == 1) { return 0; };
auto lambda3 = [] requires (sizeof(char) == 1) { };
// expected-error@-1{{lambda requires '()' before 'requires' clause}}