Debian Codegraph @

clang/test/CXX/drs/cwg7xx.cpp

raw
1 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++98 %s -verify=expected,cxx98-14,cxx98-11 -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++11 %s -verify=expected,cxx98-14,cxx98-11,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++14 %s -verify=expected,cxx98-14,since-cxx14,since-cxx11,cxx14 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++2a %s -verify=expected,since-cxx14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6
7 namespace cwg705 { // cwg705: yes
8 namespace N {
9 struct S {};
10 void f(S); // #cwg705-f
11 }
12
13 void g() {
14 N::S s;
15 f(s); // ok
16 (f)(s);
17 // expected-error@-1 {{use of undeclared identifier 'f'}}
18 // expected-note@#cwg705-f {{'N::f' declared here}}
19 }
20 }
21
22 namespace cwg712 { // cwg712: partial
23 void use(int);
24 void f() {
25 const int a = 0; // #cwg712-f-a
26 struct X {
27 void g(bool cond) {
28 use(a);
29 use((a));
30 use(cond ? a : a);
31 use((cond, a));
32 // expected-warning@-1 {{left operand of comma operator has no effect}}
33
34 (void)a;
35 // expected-error@-1 {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME
36 // expected-note@#cwg712-f-a {{'a' declared here}}
37 (void)(a);
38 // expected-error@-1 {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME
39 // expected-note@#cwg712-f-a {{'a' declared here}}
40 (void)(cond ? a : a); // #cwg712-ternary
41 // expected-error@#cwg712-ternary {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME
42 // expected-note@#cwg712-f-a {{'a' declared here}}
43 // expected-error@#cwg712-ternary {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME
44 // expected-note@#cwg712-f-a {{'a' declared here}}
45 (void)(cond, a); // #cwg712-comma
46 // expected-error@-1 {{reference to local variable 'a' declared in enclosing function 'cwg712::f'}} FIXME
47 // expected-note@#cwg712-f-a {{'a' declared here}}
48 // expected-warning@#cwg712-comma {{left operand of comma operator has no effect}}
49 }
50 };
51 }
52
53 #if __cplusplus >= 201103L
54 void g() {
55 struct A { int n; };
56 constexpr A a = {0}; // #cwg712-g-a
57 struct X {
58 void g(bool cond) {
59 use(a.n);
60 use(a.*&A::n);
61
62 (void)a.n;
63 // since-cxx11-error@-1 {{reference to local variable 'a' declared in enclosing function 'cwg712::g'}} FIXME
64 // since-cxx11-note@#cwg712-g-a {{'a' declared here}}
65 (void)(a.*&A::n);
66 // since-cxx11-error@-1 {{reference to local variable 'a' declared in enclosing function 'cwg712::g'}} FIXME
67 // since-cxx11-note@#cwg712-g-a {{'a' declared here}}
68 }
69 };
70 }
71 #endif
72 }
73
74 namespace cwg727 { // cwg727: partial
75 struct A {
76 template<typename T> struct C; // #cwg727-C
77 template<typename T> void f(); // #cwg727-f
78 template<typename T> static int N; // #cwg727-N
79 // cxx98-11-error@-1 {{variable templates are a C++14 extension}}
80
81 template<> struct C<int>;
82 template<> void f<int>();
83 template<> int N<int>;
84
85 template<typename T> struct C<T*>;
86 template<typename T> static int N<T*>;
87
88 struct B {
89 template<> struct C<float>;
90 // expected-error@-1 {{class template specialization of 'C' not in class 'A' or an enclosing namespace}}
91 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
92 template<> void f<float>();
93 // expected-error@-1 {{no function template matches function template specialization 'f'}}
94 template<> int N<float>;
95 // expected-error@-1 {{variable template specialization of 'N' not in class 'A' or an enclosing namespace}}
96 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
97
98 template<typename T> struct C<T**>;
99 // expected-error@-1 {{class template partial specialization of 'C' not in class 'A' or an enclosing namespace}}
100 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
101 template<typename T> static int N<T**>;
102 // expected-error@-1 {{variable template partial specialization of 'N' not in class 'A' or an enclosing namespace}}
103 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
104
105 template<> struct A::C<double>;
106 // expected-error@-1 {{non-friend class member 'C' cannot have a qualified name}}
107 // expected-error@-2 {{class template specialization of 'C' not in class 'A' or an enclosing namespace}}
108 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
109 template<> void A::f<double>();
110 // expected-error@-1 {{o function template matches function template specialization 'f'}}
111 // expected-error@-2 {{non-friend class member 'f' cannot have a qualified name}}
112 template<> int A::N<double>;
113 // expected-error@-1 {{non-friend class member 'N' cannot have a qualified name}}
114 // expected-error@-2 {{variable template specialization of 'N' not in class 'A' or an enclosing namespace}}
115 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
116
117 template<typename T> struct A::C<T***>;
118 // expected-error@-1 {{non-friend class member 'C' cannot have a qualified name}}
119 // expected-error@-2 {{class template partial specialization of 'C' not in class 'A' or an enclosing namespace}}
120 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
121 template<typename T> static int A::N<T***>;
122 // expected-error@-1 {{non-friend class member 'N' cannot have a qualified name}}
123 // expected-error@-2 {{variable template partial specialization of 'N' not in class 'A' or an enclosing namespace}}
124 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
125 };
126 };
127
128 template<> struct A::C<char>;
129 template<> void A::f<char>();
130 template<> int A::N<char>;
131
132 template<typename T> struct A::C<T****>;
133 template<typename T> int A::N<T****>;
134
135 namespace C {
136 template<> struct A::C<long>;
137 // expected-error@-1 {{class template specialization of 'C' not in class 'A' or an enclosing namespace}}
138 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
139 template<> void A::f<long>();
140 // expected-error@-1 {{function template specialization of 'f' not in class 'A' or an enclosing namespace}}
141 // expected-note@#cwg727-f {{explicitly specialized declaration is here}}
142 template<> int A::N<long>;
143 // expected-error@-1 {{variable template specialization of 'N' not in class 'A' or an enclosing namespace}}
144 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
145
146 template<typename T> struct A::C<T*****>;
147 // expected-error@-1 {{class template partial specialization of 'C' not in class 'A' or an enclosing namespace}}
148 // expected-note@#cwg727-C {{explicitly specialized declaration is here}}
149 template<typename T> int A::N<T*****>;
150 // expected-error@-1 {{variable template partial specialization of 'N' not in class 'A' or an enclosing namespace}}
151 // expected-note@#cwg727-N {{explicitly specialized declaration is here}}
152 }
153
154 template<typename>
155 struct D {
156 template<typename T> struct C { typename T::error e; };
157 // expected-error@-1 {{type 'float' cannot be used prior to '::' because it has no members}}
158 // expected-note@#cwg727-C-float {{in instantiation of template class 'cwg727::D<int>::C<float>' requested here}}
159 template<typename T> void f() { T::error; }
160 // expected-error@-1 {{type 'float' cannot be used prior to '::' because it has no members}}
161 // expected-note@#cwg727-f-float {{in instantiation of function template specialization 'cwg727::D<int>::f<float>' requested here}}
162 template<typename T> static const int N = T::error;
163 // cxx98-11-error@-1 {{variable templates are a C++14 extension}}
164 // expected-error@-2 {{type 'float' cannot be used prior to '::' because it has no members}}
165 // expected-note@#cwg727-N-float {{in instantiation of static data member 'cwg727::D<int>::N<float>' requested here}}
166
167 template<> struct C<int> {};
168 template<> void f<int>() {}
169 template<> const int N<int>;
170
171 template<typename T> struct C<T*> {};
172 template<typename T> static const int N<T*>;
173
174 template<typename>
175 struct E {
176 template<> void f<void>() {}
177 // expected-error@-1 {{no candidate function template was found for dependent member function template specialization}}
178 };
179 };
180
181 void d(D<int> di) {
182 D<int>::C<int>();
183 di.f<int>();
184 int a = D<int>::N<int>;
185
186 D<int>::C<int*>();
187 int b = D<int>::N<int*>;
188
189 D<int>::C<float>(); // #cwg727-C-float
190 di.f<float>(); // #cwg727-f-float
191 int c = D<int>::N<float>; // #cwg727-N-float
192 }
193
194 namespace mixed_inner_outer_specialization {
195 #if __cplusplus >= 201103L
196 template<int> struct A {
197 template<int> constexpr int f() const { return 1; }
198 template<> constexpr int f<0>() const { return 2; }
199 };
200 template<> template<int> constexpr int A<0>::f() const { return 3; }
201 template<> template<> constexpr int A<0>::f<0>() const { return 4; }
202 static_assert(A<1>().f<1>() == 1, "");
203 static_assert(A<1>().f<0>() == 2, "");
204 static_assert(A<0>().f<1>() == 3, "");
205 static_assert(A<0>().f<0>() == 4, "");
206 #endif
207
208 #if __cplusplus >= 201402L
209 template<int> struct B {
210 template<int> static const int u = 1;
211 template<> const int u<0> = 2; // #cwg727-u0
212
213 // Note that in C++17 onwards, these are implicitly inline, and so the
214 // initializer of v<0> is not instantiated with the declaration. In
215 // C++14, v<0> is a non-defining declaration and its initializer is
216 // instantiated with the class.
217 template<int> static constexpr int v = 1;
218 template<> constexpr int v<0> = 2; // #cwg727-v0
219
220 template<int> static const inline int w = 1;
221 // cxx14-error@-1 {{inline variables are a C++17 extension}}
222 template<> const inline int w<0> = 2;
223 // cxx14-error@-1 {{inline variables are a C++17 extension}}
224 };
225
226 template<> template<int> constexpr int B<0>::u = 3;
227 template<> template<> constexpr int B<0>::u<0> = 4;
228 // since-cxx14-error@-1 {{static data member 'u' already has an initializer}}
229 // since-cxx14-note@#cwg727-u0 {{previous initialization is here}}
230
231 template<> template<int> constexpr int B<0>::v = 3;
232 template<> template<> constexpr int B<0>::v<0> = 4;
233 // cxx14-error@-1 {{static data member 'v' already has an initializer}}
234 // cxx14-note@#cwg727-v0 {{previous initialization is here}}
235
236 template<> template<int> constexpr int B<0>::w = 3;
237 template<> template<> constexpr int B<0>::w<0> = 4;
238
239 static_assert(B<1>().u<1> == 1, "");
240 static_assert(B<1>().u<0> == 2, "");
241 static_assert(B<0>().u<1> == 3, "");
242
243 static_assert(B<1>().v<1> == 1, "");
244 static_assert(B<1>().v<0> == 2, "");
245 static_assert(B<0>().v<1> == 3, "");
246 static_assert(B<0>().v<0> == 4, "");
247 // cxx14-error@-1 {{static assertion failed due to requirement 'cwg727::mixed_inner_outer_specialization::B<0>().v<0> == 4'}}
248 // cxx14-note@-2 {{expression evaluates to '2 == 4'}}
249
250 static_assert(B<1>().w<1> == 1, "");
251 static_assert(B<1>().w<0> == 2, "");
252 static_assert(B<0>().w<1> == 3, "");
253 static_assert(B<0>().w<0> == 4, "");
254 #endif
255 }
256
257 template<typename T, typename U> struct Collision {
258 // FIXME: Missing diagnostic for duplicate function explicit specialization declaration.
259 template<typename> int f1();
260 template<> int f1<T>();
261 template<> int f1<U>();
262
263 // FIXME: Missing diagnostic for fucntion redefinition!
264 template<typename> int f2();
265 template<> int f2<T>() {}
266 template<> int f2<U>() {}
267
268 template<typename> static int v1;
269 // cxx98-11-error@-1 {{variable templates are a C++14 extension}}
270 template<> int v1<T>; // #cwg727-v1-T
271 template<> int v1<U>;
272 // expected-error@-1 {{duplicate member 'v1'}}
273 // expected-note@#cwg727-Collision-int-int {{in instantiation of template class 'cwg727::Collision<int, int>' requested here}}
274 // expected-note@#cwg727-v1-T {{previous}}
275
276 template<typename> static inline int v2;
277 // cxx98-11-error@-1 {{variable templates are a C++14 extension}}
278 // cxx98-14-error@-2 {{inline variables are a C++17 extension}}
279 template<> inline int v2<T>; // #cwg727-v2-T
280 // cxx98-14-error@-1 {{inline variables are a C++17 extension}}
281 template<> inline int v2<U>;
282 // cxx98-14-error@-1 {{inline variables are a C++17 extension}}
283 // expected-error@-2 {{duplicate member 'v2'}}
284 // expected-note@#cwg727-v2-T {{previous declaration is here}}
285
286 // FIXME: Missing diagnostic for duplicate class explicit specialization.
287 template<typename> struct S1;
288 template<> struct S1<T>;
289 template<> struct S1<U>;
290
291 template<typename> struct S2;
292 template<> struct S2<T> {}; // #cwg727-S2-T
293 template<> struct S2<U> {};
294 // expected-error@-1 {{redefinition of 'S2<int>'}}
295 // expected-note@#cwg727-S2-T {{previous}}
296 };
297 Collision<int, int> c; // #cwg727-Collision-int-int
298 }
299
300 namespace cwg777 { // cwg777: 3.7
301 #if __cplusplus >= 201103L
302 template <typename... T>
303 void f(int i = 0, T ...args) {}
304 void ff() { f(); }
305
306 template <typename... T>
307 void g(int i = 0, T ...args, T ...args2) {}
308
309 template <typename... T>
310 void h(int i = 0, T ...args, int j = 1) {}
311 #endif
312 }