[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg13xx.cpp
blob416de7c536b1a467c40ea1570b42a3745e34df96
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98-14,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx11-20,cxx11-17,cxx11-14,cxx98-14,since-cxx11,cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx11-20,cxx11-17,cxx11-14,since-cxx14,cxx98-14,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,cxx11-20,cxx11-17,since-cxx14,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,cxx11-20,since-cxx14,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx14,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx14,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
9 __extension__ typedef __SIZE_TYPE__ size_t;
11 namespace std {
12 template<typename T> struct initializer_list {
13 const T *ptr;
14 size_t n;
15 initializer_list(const T*, size_t);
19 #if __cplusplus >= 201103L
20 namespace cwg1305 { // cwg1305: 3.0
21 struct Incomplete; // #cwg1305-Incomplete
22 struct Complete {};
24 int incomplete = alignof(Incomplete(&)[]);
25 // since-cxx11-error@-1 {{invalid application of 'alignof' to an incomplete type 'Incomplete'}}
26 // since-cxx11-note@#cwg1305-Incomplete {{forward declaration of 'cwg1305::Incomplete'}}
27 int complete = alignof(Complete(&)[]);
29 #endif
31 namespace cwg1307 { // cwg1307: 14
32 #if __cplusplus >= 201103L
33 void f(int const (&)[2]);
34 void f(int const (&)[3]);
36 void caller() {
37 // This should not be ambiguous, the 2nd overload is better.
38 f({1, 2, 3});
40 #endif // __cplusplus >= 201103L
41 } // namespace cwg1307
43 // cwg1308: sup 1330
45 namespace cwg1310 { // cwg1310: 5
46 struct S {} * sp = new S::S;
47 // expected-error@-1 {{qualified reference to 'S' is a constructor name rather than a type in this context}}
48 void f() {
49 S::S(a);
50 // expected-error@-1 {{qualified reference to 'S' is a constructor name rather than a type in this context}}
52 struct T { int n; typedef int U; typedef T V; };
53 int k = T().T::T::n;
54 T::V v;
56 struct U { int U; };
57 int u = U().U::U;
58 struct U::U w;
60 struct V : T::T {
61 // FIXME: This is technically ill-formed, but we consider that to be a defect.
62 V() : T::T() {}
64 template<typename T> struct VT : T::T {
65 VT() : T::T() {}
67 template struct VT<T>;
69 template<template<typename> class> class TT {};
70 template<typename> class TTy {};
72 template<typename T> struct WBase {};
73 template<typename T> struct W : WBase<T> { typedef int X; int n; };
75 void w_test() {
76 W<int>::W w1a;
77 // expected-error@-1 {{qualified reference to 'W' is a constructor name rather than a type in this context}}
78 W<int>::W::X w1ax;
79 W<int>::W<int> w1b;
80 // expected-error@-1 {{qualified reference to 'W' is a constructor name rather than a template name in this context}}
81 W<int>::W<int>::X w1bx;
82 typename W<int>::W w2a;
83 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a type in this context, despite preceding 'typename' keyword}}
84 // cxx98-error@-2 {{'typename' occurs outside of a template}}
85 typename W<int>::W::X w2ax;
86 // cxx98-error@-1 {{'typename' occurs outside of a template}}
87 typename W<int>::W<int> w2b;
88 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'typename' keyword}}
89 // cxx98-error@-2 {{'typename' occurs outside of a template}}
90 typename W<int>::W<int>::X w2bx;
91 // cxx98-error@-1 {{'typename' occurs outside of a template}}
92 W<int>::template W<int> w3;
93 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'template' keyword}}
94 // cxx98-error@-2 {{'template' keyword outside of a template}}
95 W<int>::template W<int>::X w3x;
96 // cxx98-error@-1 {{'template' keyword outside of a template}}
97 typename W<int>::template W<int> w4;
98 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'template' keyword}}
99 // cxx98-error@-2 {{'template' keyword outside of a template}}
100 // cxx98-error@-3 {{'typename' occurs outside of a template}}
101 typename W<int>::template W<int>::X w4x;
102 // cxx98-error@-1 {{'template' keyword outside of a template}}
103 // cxx98-error@-2 {{'typename' occurs outside of a template}}
105 TT<W<int>::W> tt1;
106 // expected-error@-1 {{qualified reference to 'W' is a constructor name rather than a type in this context}}
107 TTy<W<int>::W> tt1a;
108 // expected-error@-1 {{qualified reference to 'W' is a constructor name rather than a type in this context}}
109 TT<W<int>::template W> tt2;
110 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'template' keyword}}
111 // cxx98-error@-2 {{'template' keyword outside of a template}}
112 TT<W<int>::WBase> tt3;
113 TTy<W<int>::WBase> tt3a;
114 TT<W<int>::template WBase> tt4;
115 // cxx98-error@-1 {{'template' keyword outside of a template}}
117 W<int> w;
118 (void)w.W::W::n;
119 (void)w.W<int>::W::n;
120 (void)w.W<int>::W<int>::n;
121 (void)w.W<int>::template W<int>::n;
122 // cxx98-error@-1 {{'template' keyword outside of a template}}
125 template<typename W>
126 void wt_test() {
127 typename W::W w2a;
128 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a type in this context, despite preceding 'typename' keyword}}
129 // cxx98-note@#cwg1310-W-int {{in instantiation of function template specialization 'cwg1310::wt_test<cwg1310::W<int> >' requested here}}
130 // since-cxx11-note@#cwg1310-W-int {{in instantiation of function template specialization 'cwg1310::wt_test<cwg1310::W<int>>' requested here}}
131 typename W::template W<int> w4;
132 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'template' keyword}}
133 TTy<typename W::W> tt2;
134 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a type in this context, despite preceding 'typename' keyword}}
135 TT<W::template W> tt3;
136 // expected-error@-1 {{ISO C++ specifies that qualified reference to 'W' is a constructor name rather than a template name in this context, despite preceding 'template' keyword}}
138 template<typename W>
139 void wt_test_good() {
140 typename W::W::X w2ax;
141 typename W::template W<int>::X w4x;
142 TTy<typename W::WBase> tt4;
143 TT<W::template WBase> tt5;
145 W w;
146 (void)w.W::W::n;
147 (void)w.W::template W<int>::n;
148 (void)w.template W<int>::W::n;
149 (void)w.template W<int>::template W<int>::n;
151 template void wt_test<W<int> >(); // #cwg1310-W-int
152 template void wt_test_good<W<int> >();
155 namespace cwg1315 { // cwg1315: partial
156 template <int I, int J> struct A {};
157 template <int I> struct A<I + 5, I * 2> {};
158 // expected-error@-1 {{class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used}}
159 // expected-note@-2 {{non-deducible template parameter 'I'}}
160 template <int I> struct A<I, I> {};
162 template <int I, int J, int K> struct B;
163 template <int I, int K> struct B<I, I * 2, K> {}; // #cwg1315-B-1
164 B<1, 2, 3> b1;
166 // Multiple declarations with the same dependent expression are equivalent
167 // for partial ordering purposes.
168 template <int I> struct B<I, I * 2, 2> { typedef int type; };
169 B<1, 2, 2>::type b2;
171 // Multiple declarations with differing dependent expressions are unordered.
172 template <int I, int K> struct B<I, I + 1, K> {}; // #cwg1315-B-2
173 B<1, 2, 4> b3;
174 // expected-error@-1 {{ambiguous partial specializations of 'B<1, 2, 4>'}}
175 // expected-note@#cwg1315-B-1 {{partial specialization matches [with I = 1, K = 4]}}
176 // expected-note@#cwg1315-B-2 {{partial specialization matches [with I = 1, K = 4]}}
178 // FIXME: Under cwg1315, this is perhaps valid, but that is not clear: this
179 // fails the "more specialized than the primary template" test because the
180 // dependent type of T::value is not the same as 'int'.
181 // A core issue will be opened to decide what is supposed to happen here.
182 template <typename T, int I> struct C;
183 template <typename T> struct C<T, T::value>;
184 // expected-error@-1 {{type of specialized non-type template argument depends on a template parameter of the partial specialization}}
187 namespace cwg1330 { // cwg1330: 4 c++11
188 // exception-specifications are parsed in a context where the class is complete.
189 struct A {
190 void f() throw(T) {}
191 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
192 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
193 struct T {};
195 #if __cplusplus >= 201103L
196 void g() noexcept(&a == b) {}
197 static int a;
198 static constexpr int *b = &a;
199 #endif
202 void (A::*af1)() throw(A::T) = &A::f;
203 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
204 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
205 void (A::*af2)() throw() = &A::f;
206 // cxx98-14-error@-1 {{target exception specification is not superset of source}}
207 // since-cxx17-error@-2 {{cannot initialize a variable of type 'void (cwg1330::A::*)() throw()' with an rvalue of type 'void (cwg1330::A::*)() throw(T)': different exception specifications}}
209 #if __cplusplus >= 201103L
210 static_assert(noexcept(A().g()), "");
211 #endif
213 // Likewise, they're instantiated separately from an enclosing class template.
214 template<typename U>
215 struct B {
216 void f() throw(T, typename U::type) {}
217 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
218 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
219 struct T {};
221 #if __cplusplus >= 201103L
222 void g() noexcept(&a == b && U::value) {}
223 static int a;
224 static constexpr int *b = &a;
225 #endif
228 B<int> bi; // ok
230 struct P {
231 typedef int type;
232 static const int value = true;
235 // FIXME: We only delay instantiation in C++11 onwards. In C++98, something
236 // weird happens: instantiation of B<P> fails because it references T before
237 // it's instantiated, but the diagnostic is suppressed in
238 // Sema::FindInstantiatedDecl because we've already hit an error. This is
239 // obviously a bad way to react to this situation; we should still producing
240 // the "T has not yet been instantiated" error here, rather than giving
241 // confusing errors later on.
242 void (B<P>::*bpf1)() throw(B<P>::T, int) = &B<P>::f;
243 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
244 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
245 // cxx98-error@-3 {{target exception specification is not superset of source}}
247 void (B<P>::*bpf2)() throw(int) = &B<P>::f;
248 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
249 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
250 // cxx98-14-error@-3 {{target exception specification is not superset of source}}
251 // since-cxx17-warning@-4 {{target exception specification is not superset of source}}
252 void (B<P>::*bpf3)() = &B<P>::f;
253 void (B<P>::*bpf4)() throw() = &B<P>::f;
254 // cxx98-14-error@-1 {{target exception specification is not superset of source}}
255 // since-cxx17-error@-2 {{cannot initialize a variable of type 'void (B<P>::*)() throw()' with an rvalue of type 'void (cwg1330::B<cwg1330::P>::*)() throw(T, typename P::type)': different exception specifications}}
257 #if __cplusplus >= 201103L
258 static_assert(noexcept(B<P>().g()), "");
259 struct Q { static const int value = false; };
260 static_assert(!noexcept(B<Q>().g()), "");
261 #endif
263 template<typename T> int f() throw(typename T::error) { return 0; } // #cwg1330-f
264 // expected-error@#cwg1330-f {{type 'int' cannot be used prior to '::' because it has no members}}
265 // cxx98-note@#cwg1330-f-int {{in instantiation of function template specialization 'cwg1330::f<int>' requested here}}
266 // since-cxx11-note@#cwg1330-f-int {{in instantiation of exception specification for 'f<int>' requested here}}
267 // cxx98-14-error@#cwg1330-f {{type 'short' cannot be used prior to '::' because it has no members}}
268 // cxx98-14-note@#cwg1330-f-short {{in instantiation of function template specialization 'cwg1330::f<short>' requested here}}
269 // cxx11-14-note@#cwg1330-f {{in instantiation of exception specification for 'f<short>' requested here}}
270 // since-cxx11-error@#cwg1330-f {{type 'char' cannot be used prior to '::' because it has no members}}
271 // since-cxx11-note@#cwg1330-f-char {{in instantiation of exception specification for 'f<char>' requested here}}
272 // since-cxx11-error@#cwg1330-f {{type 'float' cannot be used prior to '::' because it has no members}}
273 // since-cxx11-note@#cwg1330-f-float {{in instantiation of exception specification for 'f<float>' requested here}}
274 // since-cxx17-error@#cwg1330-f {{ISO C++17 does not allow dynamic exception specifications}}
275 // since-cxx17-note@#cwg1330-f {{use 'noexcept(false)' instead}}
277 // An exception-specification is needed even if the function is only used in
278 // an unevaluated operand.
279 int f1 = sizeof(f<int>()); // #cwg1330-f-int
280 #if __cplusplus >= 201103L
281 decltype(f<char>()) f2; // #cwg1330-f-char
282 bool f3 = noexcept(f<float>()); /// #cwg1330-f-float
283 #endif
284 template int f<short>(); // #cwg1330-f-short
285 // since-cxx17-error@#cwg1330-f {{type 'short' cannot be used prior to '::' because it has no members}}
286 // since-cxx17-note@#cwg1330-f {{in instantiation of exception specification for 'f<short>' requested here}}
287 // since-cxx17-note@#cwg1330-f-short {{in instantiation of function template specialization 'cwg1330::f<short>' requested here}}
289 template<typename T> struct C {
290 C() throw(typename T::type); // #cwg1330-C
291 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}
292 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}
293 // cxx98-error@#cwg1330-C {{type 'void' cannot be used prior to '::' because it has no members}}
294 // cxx98-note@#cwg1330-C-void {{in instantiation of template class 'cwg1330::C<void>' requested here}}
295 // expected-error@#cwg1330-C {{type 'int' cannot be used prior to '::' because it has no members}}
296 // cxx98-note@#cwg1330-C-int {{in instantiation of template class 'cwg1330::C<int>' requested here}}
297 // since-cxx11-note@#cwg1330-C-int {{in instantiation of exception specification for 'C' requested here}}
298 // since-cxx11-note@#cwg1330-e {{in evaluation of exception specification for 'cwg1330::E::E' needed here}}
300 struct D : C<void> {}; // #cwg1330-C-void
301 void f(D &d) { d = d; } // ok
303 struct E : C<int> {}; // #cwg1330-C-int
304 E e; // #cwg1330-e
307 // cwg1334: sup 1719
309 namespace cwg1340 { // cwg1340: 2.9
310 struct A;
311 struct B;
313 void f(B* a, A B::* p) {
314 (*a).*p;
315 // expected-warning@-1 {{expression result unused}}
316 a->*p;
317 // expected-warning@-1 {{expression result unused}}
319 } // namespace cwg1340
321 namespace cwg1341 { // cwg1341: sup P0683R1
322 #if __cplusplus >= 202002L
323 int a;
324 const int b = 0; // #cwg1341-b
325 struct S {
326 int x1 : 8 = 42;
327 int x2 : 8 { 42 };
328 int y1 : true ? 8 : a = 42;
329 int y2 : true ? 8 : b = 42;
330 // since-cxx20-error@-1 {{cannot assign to variable 'b' with const-qualified type 'const int'}}
331 // since-cxx20-note@#cwg1341-b {{variable 'b' declared const here}}
332 int y3 : (true ? 8 : b) = 42;
333 int z : 1 || new int { 0 };
335 #endif
338 namespace cwg1346 { // cwg1346: 3.5
339 auto a(1);
340 // cxx98-error@-1 {{'auto' type specifier is a C++11 extension}}
341 auto b(1, 2);
342 // cxx98-error@-1 {{'auto' type specifier is a C++11 extension}}
343 // expected-error@-2 {{initializer for variable 'b' with type 'auto' contains multiple expressions}}
344 #if __cplusplus >= 201103L
345 auto c({});
346 // since-cxx11-error@-1 {{cannot deduce type for variable 'c' with type 'auto' from parenthesized initializer list}}
347 auto d({1});
348 // since-cxx11-error@-1 {{cannot deduce type for variable 'd' with type 'auto' from parenthesized initializer list}}
349 auto e({1, 2});
350 // since-cxx11-error@-1 {{cannot deduce type for variable 'e' with type 'auto' from parenthesized initializer list}}
351 #endif
352 template<typename...Ts> void f(Ts ...ts) {
353 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
354 auto x(ts...);
355 // cxx98-error@-1 {{'auto' type specifier is a C++11 extension}}
356 // expected-error@-2 {{initializer for variable 'x' with type 'auto' is empty}}
357 // expected-note@#cwg1346-f {{in instantiation of function template specialization 'cwg1346::f<>' requested here}}
359 template void f(); // #cwg1346-f
361 #if __cplusplus >= 201103L
362 void init_capture() {
363 [a(1)] {} ();
364 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
365 [b(1, 2)] {} ();
366 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
367 // since-cxx11-error@-2 {{initializer for lambda capture 'b' contains multiple expressions}}
368 [c({})] {} ();
369 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
370 // since-cxx11-error@-2 {{cannot deduce type for lambda capture 'c' from parenthesized initializer list}}
371 [d({1})] {} ();
372 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
373 // since-cxx11-error@-2 {{cannot deduce type for lambda capture 'd' from parenthesized initializer list}}
374 [e({1, 2})] {} ();
375 // cxx11-error@-1 {{initialized lambda captures are a C++14 extension}}
376 // since-cxx11-error@-2 {{cannot deduce type for lambda capture 'e' from parenthesized initializer list}}
378 #endif
381 namespace cwg1347 { // cwg1347: 3.1
382 auto x = 5, *y = &x;
383 // cxx98-error@-1 {{'auto' type specifier is a C++11 extension}}
384 auto z = y, *q = y;
385 // cxx98-error@-1 {{'auto' type specifier is a C++11 extension}}
386 // expected-error@-2 {{'auto' deduced as 'int *' in declaration of 'z' and deduced as 'int' in declaration of 'q'}}
387 #if __cplusplus >= 201103L
388 auto a = 5, b = {1, 2};
389 // since-cxx11-error@-1 {{'auto' deduced as 'int' in declaration of 'a' and deduced as 'std::initializer_list<int>' in declaration of 'b'}}
390 auto (*fp)(int) -> int, i = 0;
391 // since-cxx11-error@-1 {{declaration with trailing return type must be the only declaration in its group}}
392 #endif
395 namespace cwg1350 { // cwg1350: 3.5
396 #if __cplusplus >= 201103L
397 struct NoexceptCtor {
398 NoexceptCtor(int) noexcept {}
401 struct ThrowingNSDMI : NoexceptCtor {
402 int a = []() noexcept(false) { return 0; }();
403 using NoexceptCtor::NoexceptCtor;
406 static_assert(!__is_nothrow_constructible(ThrowingNSDMI, int), "");
408 struct ThrowingCtor {
409 ThrowingCtor() noexcept(false) {}
412 struct ThrowingNSDM : NoexceptCtor {
413 ThrowingCtor c;
414 using NoexceptCtor::NoexceptCtor;
417 static_assert(!__is_nothrow_constructible(ThrowingNSDM, int), "");
419 struct ThrowingCtorTemplate {
420 template <typename = int>
421 ThrowingCtorTemplate() noexcept(false) {}
424 struct ThrowingNSDM2 : NoexceptCtor {
425 ThrowingCtorTemplate c;
426 using NoexceptCtor::NoexceptCtor;
429 static_assert(!__is_nothrow_constructible(ThrowingNSDM2, int), "");
431 struct D1 : NoexceptCtor, ThrowingCtor {
432 using NoexceptCtor::NoexceptCtor;
435 static_assert(!__is_nothrow_constructible(D1, int), "");
437 struct D2 : NoexceptCtor, ThrowingCtorTemplate {
438 using NoexceptCtor::NoexceptCtor;
441 static_assert(!__is_nothrow_constructible(D2, int), "");
443 struct ThrowingDefaultArg {
444 ThrowingDefaultArg(ThrowingCtor = {}) {}
447 struct D3 : NoexceptCtor, ThrowingDefaultArg {
448 using NoexceptCtor::NoexceptCtor;
451 static_assert(!__is_nothrow_constructible(D3, int), "");
453 struct ThrowingDefaultArgTemplate {
454 template <typename = int>
455 ThrowingDefaultArgTemplate(ThrowingCtor = {}) {}
458 struct D4 : NoexceptCtor, ThrowingDefaultArgTemplate {
459 using NoexceptCtor::NoexceptCtor;
462 static_assert(!__is_nothrow_constructible(D4, int), "");
463 #endif
464 } // namespace cwg1350
466 namespace cwg1352 { // cwg1352: 3.0
467 struct A {
468 #if __cplusplus >= 201103L
469 int a = sizeof(A);
470 #endif
471 void f(int b = sizeof(A));
474 template <typename T>
475 struct B {
476 #if __cplusplus >= 201103L
477 int a = sizeof(B) + sizeof(T);
478 #endif
479 void f(int b = sizeof(B) + sizeof(T));
482 template class B<int>;
483 } // namespace cwg1352
485 namespace cwg1358 { // cwg1358: 3.1
486 #if __cplusplus >= 201103L
487 struct Lit { constexpr operator int() const { return 0; } };
488 struct NonLit { NonLit(); operator int(); }; // #cwg1358-NonLit
489 struct NonConstexprConv { constexpr operator int() const; };
490 struct Virt { virtual int f(int) const; };
492 template<typename T, typename U, typename V> struct A : V {
493 int member;
494 constexpr A(U u) : member(u) {}
495 constexpr T f(U u) const { return T(); }
498 constexpr A<Lit, Lit, Lit> ce = Lit();
499 constexpr int k = ce.f(Lit{});
501 // Can have a non-literal return type and parameter type.
502 // Constexpr function can be implicitly virtual.
503 A<NonLit, NonLit, Virt> a = NonLit();
504 void g() { a.f(NonLit()); }
506 // Constructor is still constexpr, so this is a literal type.
507 static_assert(__is_literal_type(decltype(a)), "");
509 // Constructor can call non-constexpr functions.
510 A<Lit, NonConstexprConv, Lit> b = NonConstexprConv();
512 // But the corresponding non-template cases are rejected.
513 struct B : Virt {
514 int member;
515 constexpr B(NonLit u) : member(u) {}
516 // cxx11-20-error@-1 {{constexpr constructor's 1st parameter type 'NonLit' is not a literal type}}
517 // cxx11-20-note@#cwg1358-NonLit {{'NonLit' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}}
518 constexpr NonLit f(NonLit u) const { return NonLit(); }
519 // cxx11-20-error@-1 {{constexpr function's return type 'NonLit' is not a literal type}}
520 // cxx11-20-note@#cwg1358-NonLit {{'NonLit' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}}
522 #endif
525 namespace cwg1359 { // cwg1359: 3.5
526 #if __cplusplus >= 201103L
527 union A { constexpr A() = default; };
528 union B { constexpr B() = default; int a; }; // #cwg1359-B
529 // cxx11-17-error@-1 {{defaulted definition of default constructor cannot be marked constexpr before C++23}}
530 union C { constexpr C() = default; int a, b; }; // #cwg1359-C
531 // cxx11-17-error@-1 {{defaulted definition of default constructor cannot be marked constexpr}}
532 struct X { constexpr X() = default; union {}; };
533 // since-cxx11-error@-1 {{declaration does not declare anything}}
534 struct Y { constexpr Y() = default; union { int a; }; }; // #cwg1359-Y
535 // cxx11-17-error@-1 {{defaulted definition of default constructor cannot be marked constexpr}}
537 constexpr A a = A();
538 constexpr B b = B();
539 // cxx11-17-error@-1 {{no matching constructor for initialization of 'B'}}
540 // cxx11-17-note@#cwg1359-B {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}
541 // cxx11-17-note@#cwg1359-B {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}
542 constexpr C c = C();
543 // cxx11-17-error@-1 {{no matching constructor for initialization of 'C'}}
544 // cxx11-17-note@#cwg1359-C {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}
545 // cxx11-17-note@#cwg1359-C {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}
546 constexpr X x = X();
547 constexpr Y y = Y();
548 // cxx11-17-error@-1 {{no matching constructor for initialization of 'Y'}}
549 // cxx11-17-note@#cwg1359-Y {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}
550 // cxx11-17-note@#cwg1359-Y {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}
551 #endif
554 namespace cwg1388 { // cwg1388: 4
555 template<typename A, typename ...T> void f(T..., A); // #cwg1388-f
556 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
557 template<typename ...T> void g(T..., int); // #cwg1388-g
558 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
559 template<typename ...T, typename A> void h(T..., A); // #cwg1388-h
560 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
562 void test_f() {
563 f(0); // ok, trailing parameter pack deduced to empty
564 f(0, 0);
565 // expected-error@-1 {{no matching function for call to 'f'}}
566 // expected-note@#cwg1388-f {{candidate function [with A = int, T = <>] not viable: requires 1 argument, but 2 were provided}}
567 f<int>(0);
568 f<int>(0, 0);
569 // expected-error@-1 {{no matching function for call to 'f'}}
570 // expected-note@#cwg1388-f {{candidate function [with A = int, T = <>] not viable: requires 1 argument, but 2 were provided}}
571 f<int, int>(0, 0);
572 f<int, int, int>(0, 0);
573 // expected-error@-1 {{no matching function for call to 'f'}}
574 // expected-note@#cwg1388-f {{candidate function [with A = int, T = <int, int>] not viable: requires 3 arguments, but 2 were provided}}
576 g(0);
577 g(0, 0);
578 // expected-error@-1 {{no matching function for call to 'g'}}
579 // expected-note@#cwg1388-g {{candidate function [with T = <>] not viable: requires 1 argument, but 2 were provided}}
580 g<>(0);
581 g<int>(0);
582 // expected-error@-1 {{no matching function for call to 'g'}}
583 // expected-note@#cwg1388-g {{candidate function [with T = <int>] not viable: requires 2 arguments, but 1 was provided}}
584 g<int>(0, 0);
586 h(0);
587 h(0, 0);
588 // expected-error@-1 {{no matching function for call to 'h'}}
589 // expected-note@#cwg1388-h {{candidate function [with T = <>, A = int] not viable: requires 1 argument, but 2 were provided}}
590 h<int>(0, 0);
591 h<int, int>(0, 0);
592 // expected-error@-1 {{no matching function for call to 'h'}}
593 // expected-note@#cwg1388-h {{candidate template ignored: couldn't infer template argument 'A'}}
596 // A non-trailing parameter pack is still a non-deduced context, even though
597 // we know exactly how many arguments correspond to it.
598 template<typename T, typename U> struct pair {};
599 template<typename ...T> struct tuple { typedef char type; }; //
600 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
601 template<typename ...T, typename ...U> void f_pair_1(pair<T, U>..., int); // #cwg1388-f-1
602 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
603 // cxx98-error@-2 {{variadic templates are a C++11 extension}}
604 template<typename ...T, typename U> void f_pair_2(pair<T, char>..., U);
605 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
606 template<typename ...T, typename ...U> void f_pair_3(pair<T, U>..., tuple<U...>); // #cwg1388-f-3
607 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
608 // cxx98-error@-2 {{variadic templates are a C++11 extension}}
609 template<typename ...T> void f_pair_4(pair<T, char>..., T...); // #cwg1388-f-4
610 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
611 void g(pair<int, char> a, pair<long, char> b, tuple<char, char> c) {
612 f_pair_1<int, long>(a, b, 0);
613 // expected-error@-1 {{no matching function for call to 'f_pair_1'}}
614 // expected-note@#cwg1388-f-1 {{candidate template ignored: substitution failure [with T = <int, long>]: deduced incomplete pack <(no value), (no value)> for template parameter 'U'}}
615 f_pair_2<int, long>(a, b, 0);
616 f_pair_3<int, long>(a, b, c);
617 f_pair_3<int, long>(a, b, tuple<char>());
618 // expected-error@-1 {{no matching function for call to 'f_pair_3'}}
619 // expected-note@#cwg1388-f-3 {{candidate template ignored: deduced packs of different lengths for parameter 'U' (<(no value), (no value)> vs. <char>)}}
620 f_pair_4<int, long>(a, b, 0, 0L);
621 f_pair_4<int, long>(a, b, 0, 0L, "foo");
622 // expected-error@-1 {{no matching function for call to 'f_pair_4'}}
623 // expected-note@#cwg1388-f-4 {{candidate template ignored: deduced packs of different lengths for parameter 'T' (<int, long> vs. <int, long, const char *>)}}
627 namespace cwg1391 { // cwg1391: partial
628 struct A {}; struct B : A {};
629 template<typename T> struct C { C(int); typename T::error error; }; // #cwg1391-C
630 // expected-error@#cwg1391-C {{type 'int' cannot be used prior to '::' because it has no members}}
631 // expected-note@#cwg1391-b {{in instantiation of template class 'cwg1391::C<int>' requested here}}
632 // expected-note@#cwg1391-b {{while substituting deduced template arguments into function template 'b' [with T = int]}}
633 // expected-error@#cwg1391-C {{type 'double' cannot be used prior to '::' because it has no members}}
634 // expected-note@#cwg1391-c {{in instantiation of template class 'cwg1391::C<double>' requested here}}
635 template<typename T> struct D {};
637 // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match.
638 template<typename T> void a(T, int T::*);
639 void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match
641 namespace dr_example_1 {
642 template<typename T, typename U> void f(C<T>);
643 template<typename T> void f(D<T>);
645 void g(D<int> d) {
646 f(d); // ok, first 'f' eliminated by deduction failure
647 f<int>(d); // ok, first 'f' eliminated because 'U' cannot be deduced
651 namespace dr_example_2 {
652 template<typename T> typename C<T>::error f(int, T);
653 template<typename T> T f(T, T);
655 void g(A a) {
656 f(a, a); // ok, no conversion from A to int for first parameter of first candidate
660 namespace std_example {
661 template<typename T> struct Z {
662 typedef typename T::x xx;
664 template<typename T> typename Z<T>::xx f(void *, T);
665 template<typename T> void f(int, T);
666 struct A {} a;
667 void g() { f(1, a); }
670 template<typename T> void b(C<int> ci, T *p);
671 void b(...);
672 void test_b() {
673 b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C<int>
674 // FIXME: The "while substituting" note should point at the overload candidate.
675 b<int>(0, 0); // #cwg1391-b
678 template<typename T> struct Id { typedef T type; };
679 template<typename T> void c(T, typename Id<C<T> >::type);
680 void test_c() {
681 // Implicit conversion sequences for dependent types are checked later.
682 c(0.0, 0); // #cwg1391-c
685 namespace partial_ordering {
686 // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored.
687 template<typename T> int a(T, short) = delete; // #cwg1391-a-short
688 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}
689 template<typename T> int a(T*, char); // #cwg1391-a-char
690 int test_a = a((int*)0, 0);
691 // expected-error@-1 {{call to 'a' is ambiguous}} FIXME
692 // expected-note@#cwg1391-a-short {{candidate function [with T = int *] has been explicitly deleted}}
693 // expected-note@#cwg1391-a-char {{candidate function [with T = int]}}
695 // FIXME: Second template should be considered more specialized:
696 // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds,
697 // deducing #2 from #1 fails to deduce T, so deduction fails.
698 template<typename T> int b(T, int) = delete; // #cwg1391-b-int
699 // cxx98-error@-1 {{deleted function definitions are a C++11 extension}}
700 template<typename T, typename U> int b(T*, U); // #cwg1391-b-U
701 int test_b = b((int*)0, 0);
702 // expected-error@-1 {{call to 'b' is ambiguous}} FIXME
703 // expected-note@#cwg1391-b-int {{candidate function [with T = int *] has been explicitly deleted}}
704 // expected-note@#cwg1391-b-U {{candidate function [with T = int, U = int]}}
706 // Unintended consequences: because partial ordering does not consider
707 // explicit template arguments, and deduction from a non-dependent type
708 // vacuously succeeds, a non-dependent template is less specialized than
709 // anything else!
710 // According to CWG1391, this is ambiguous!
711 template<typename T> int c(int);
712 template<typename T> int c(T);
713 int test_c1 = c(0); // ok
714 int test_c2 = c<int>(0); // FIXME: apparently ambiguous
718 namespace cwg1394 { // cwg1394: 15
719 #if __cplusplus >= 201103L
720 struct Incomplete;
721 Incomplete f(Incomplete) = delete; // well-formed
722 #endif
725 namespace cwg1395 { // cwg1395: 16
726 #if __cplusplus >= 201103L
727 template <typename T, typename... U> void f(T, U...);
728 template <typename T> void f(T);
729 void h(int i) {
730 // This is made ambiguous by cwg692, but made valid again by cwg1395.
731 f(&i);
733 #endif
736 namespace cwg1397 { // cwg1397: 3.2
737 #if __cplusplus >= 201103L
738 struct A {
739 // cxx11-error@-1 {{default member initializer for 'p' needed within definition of enclosing class 'A' outside of member functions}}
740 // cxx11-note@#cwg1397-p {{in evaluation of exception specification for 'cwg1397::A::A' needed here}}
741 // cxx11-note@#cwg1397-p {{default member initializer declared here}}
742 void *p = A{}; // #cwg1397-p
743 // since-cxx14-error@-1 {{default member initializer for 'p' needed within definition of enclosing class 'A' outside of member functions}}
744 // since-cxx14-note@-2 {{default member initializer declared here}}
745 operator void*() const { return nullptr; }
747 #endif
748 } // namespace cwg1397
750 namespace cwg1399 { // cwg1399: dup 1388
751 template<typename ...T> void f(T..., int, T...) {} // #cwg1399-f
752 // cxx98-error@-1 {{variadic templates are a C++11 extension}}
753 void g() {
754 f(0);
755 f<int>(0, 0, 0);
756 f(0, 0, 0);
757 // expected-error@-1 {{no matching function for call to 'f'}}
758 // expected-note@#cwg1399-f {{candidate template ignored: deduced packs of different lengths for parameter 'T' (<> vs. <int, int>)}}