1 // RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6 __extension__ typedef __SIZE_TYPE__ size_t;
9 template<typename T> struct initializer_list {
12 initializer_list(const T*, size_t);
16 namespace dr1310 { // dr1310: 5
17 struct S {} * sp = new S::S; // expected-error {{qualified reference to 'S' is a constructor name}}
19 S::S(a); // expected-error {{qualified reference to 'S' is a constructor name}}
21 struct T { int n; typedef int U; typedef T V; };
30 // FIXME: This is technically ill-formed, but we consider that to be a defect.
33 template<typename T> struct VT : T::T {
36 template struct VT<T>;
38 template<template<typename> class> class TT {};
39 template<typename> class TTy {};
41 template<typename T> struct WBase {};
42 template<typename T> struct W : WBase<T> { typedef int X; int n; };
45 W<int>::W w1a; // expected-error {{qualified reference to 'W' is a constructor name}}
47 W<int>::W<int> w1b; // expected-error {{qualified reference to 'W' is a constructor name}}
48 W<int>::W<int>::X w1bx;
49 typename W<int>::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
50 typename W<int>::W::X w2ax; // expected-error 0-1{{outside of a template}}
51 typename W<int>::W<int> w2b; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
52 typename W<int>::W<int>::X w2bx; // expected-error 0-1{{outside of a template}}
53 W<int>::template W<int> w3; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
54 W<int>::template W<int>::X w3x; // expected-error 0-1{{outside of a template}}
55 typename W<int>::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-2{{outside of a template}}
56 typename W<int>::template W<int>::X w4x; // expected-error 0-2{{outside of a template}}
58 TT<W<int>::W> tt1; // expected-error {{qualified reference to 'W' is a constructor name}}
59 TTy<W<int>::W> tt1a; // expected-error {{qualified reference to 'W' is a constructor name}}
60 TT<W<int>::template W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}} expected-error 0-1{{outside of a template}}
61 TT<W<int>::WBase> tt3;
62 TTy<W<int>::WBase> tt3a;
63 TT<W<int>::template WBase> tt4; // expected-error 0-1{{outside of a template}}
68 (void)w.W<int>::W<int>::n;
69 (void)w.W<int>::template W<int>::n; // expected-error 0-1{{outside of a template}}
74 typename W::W w2a; // expected-error {{qualified reference to 'W' is a constructor name}}
75 typename W::template W<int> w4; // expected-error {{qualified reference to 'W' is a constructor name}}
76 TTy<typename W::W> tt2; // expected-error {{qualified reference to 'W' is a constructor name}}
77 TT<W::template W> tt3; // expected-error {{qualified reference to 'W' is a constructor name}}
81 typename W::W::X w2ax;
82 typename W::template W<int>::X w4x;
83 TTy<typename W::WBase> tt4;
84 TT<W::template WBase> tt5;
88 (void)w.W::template W<int>::n;
89 (void)w.template W<int>::W::n;
90 (void)w.template W<int>::template W<int>::n;
92 template void wt_test<W<int> >(); // expected-note {{instantiation of}}
93 template void wt_test_good<W<int> >();
96 namespace dr1315 { // dr1315: partial
97 template <int I, int J> struct A {};
98 template <int I> // expected-note {{non-deducible template parameter 'I'}}
99 struct A<I + 5, I * 2> {}; // expected-error {{contains a template parameter that cannot be deduced}}
100 template <int I> struct A<I, I> {};
102 template <int I, int J, int K> struct B;
103 template <int I, int K> struct B<I, I * 2, K> {}; // expected-note {{matches}}
106 // Multiple declarations with the same dependent expression are equivalent
107 // for partial ordering purposes.
108 template <int I> struct B<I, I * 2, 2> { typedef int type; };
111 // Multiple declarations with differing dependent expressions are unordered.
112 template <int I, int K> struct B<I, I + 1, K> {}; // expected-note {{matches}}
113 B<1, 2, 4> b3; // expected-error {{ambiguous}}
115 // FIXME: Under dr1315, this is perhaps valid, but that is not clear: this
116 // fails the "more specialized than the primary template" test because the
117 // dependent type of T::value is not the same as 'int'.
118 // A core issue will be opened to decide what is supposed to happen here.
119 template <typename T, int I> struct C;
120 template <typename T> struct C<T, T::value>;
121 // expected-error@-1 {{type of specialized non-type template argument depends on a template parameter of the partial specialization}}
124 namespace dr1330 { // dr1330: 4 c++11
125 // exception-specifications are parsed in a context where the class is complete.
127 void f() throw(T) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
130 #if __cplusplus >= 201103L
131 void g() noexcept(&a == b) {}
133 static constexpr int *b = &a;
137 void (A::*af1)() throw(A::T) = &A::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
138 void (A::*af2)() throw() = &A::f; // expected-error-re {{{{not superset|different exception spec}}}}
140 #if __cplusplus >= 201103L
141 static_assert(noexcept(A().g()), "");
144 // Likewise, they're instantiated separately from an enclosing class template.
147 void f() throw(T, typename U::type) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
150 #if __cplusplus >= 201103L
151 void g() noexcept(&a == b && U::value) {}
153 static constexpr int *b = &a;
161 static const int value = true;
164 void (B<P>::*bpf1)() throw(B<P>::T, int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
165 #if __cplusplus < 201103L
166 // expected-error@-2 {{not superset}}
167 // FIXME: We only delay instantiation in C++11 onwards. In C++98, something
168 // weird happens: instantiation of B<P> fails because it references T before
169 // it's instantiated, but the diagnostic is suppressed in
170 // Sema::FindInstantiatedDecl because we've already hit an error. This is
171 // obviously a bad way to react to this situation; we should still producing
172 // the "T has not yet been instantiated" error here, rather than giving
173 // confusing errors later on.
175 void (B<P>::*bpf2)() throw(int) = &B<P>::f; // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}}
176 #if __cplusplus <= 201402L
177 // expected-error@-2 {{not superset}}
179 // expected-warning@-4 {{not superset}}
181 void (B<P>::*bpf3)() = &B<P>::f;
182 void (B<P>::*bpf4)() throw() = &B<P>::f;
183 #if __cplusplus <= 201402L
184 // expected-error@-2 {{not superset}}
186 // expected-error@-4 {{different exception specifications}}
189 #if __cplusplus >= 201103L
190 static_assert(noexcept(B<P>().g()), "");
191 struct Q { static const int value = false; };
192 static_assert(!noexcept(B<Q>().g()), "");
195 template<typename T> int f() throw(typename T::error) { return 0; } // expected-error 1-4{{prior to '::'}} expected-note 0-1{{prior to '::'}} expected-note 0-1{{requested here}}
196 #if __cplusplus > 201402L
197 // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
199 // An exception-specification is needed even if the function is only used in
200 // an unevaluated operand.
201 int f1 = sizeof(f<int>()); // expected-note {{instantiation of}}
202 #if __cplusplus >= 201103L
203 decltype(f<char>()) f2; // expected-note {{instantiation of}}
204 bool f3 = noexcept(f<float>()); // expected-note {{instantiation of}}
206 // In C++17 onwards, substituting explicit template arguments into the
207 // function type substitutes into the exception specification (because it's
208 // part of the type). In earlier languages, we don't notice there's a problem
209 // until we've already started to instantiate.
210 template int f<short>();
211 #if __cplusplus >= 201703L
212 // expected-error@-2 {{does not refer to a function template}}
214 // expected-note@-4 {{instantiation of}}
217 template<typename T> struct C {
218 C() throw(typename T::type); // expected-error 1-2{{prior to '::'}}
219 #if __cplusplus > 201402L
220 // expected-error@-2 0-1{{C++17}} expected-note@-2 0-1{{noexcept}}
223 struct D : C<void> {}; // ok
224 #if __cplusplus < 201103L
225 // expected-note@-2 {{instantiation of}}
227 void f(D &d) { d = d; } // ok
229 struct E : C<int> {}; // expected-note {{in instantiation of}}
230 #if __cplusplus >= 201103L
231 E e; // expected-note {{needed here}}
235 namespace dr1346 { // dr1346: 3.5
236 auto a(1); // expected-error 0-1{{extension}}
237 auto b(1, 2); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
238 #if __cplusplus >= 201103L
239 auto c({}); // expected-error {{parenthesized initializer list}}
240 auto d({1}); // expected-error {{parenthesized initializer list}}
241 auto e({1, 2}); // expected-error {{parenthesized initializer list}}
243 template<typename...Ts> void f(Ts ...ts) { // expected-error 0-1{{extension}}
244 auto x(ts...); // expected-error {{empty}} expected-error 0-1{{extension}}
246 template void f(); // expected-note {{instantiation}}
248 #if __cplusplus >= 201103L
249 void init_capture() {
250 [a(1)] {} (); // expected-error 0-1{{extension}}
251 [b(1, 2)] {} (); // expected-error {{multiple expressions}} expected-error 0-1{{extension}}
252 #if __cplusplus >= 201103L
253 [c({})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
254 [d({1})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
255 [e({1, 2})] {} (); // expected-error {{parenthesized initializer list}} expected-error 0-1{{extension}}
261 namespace dr1347 { // dr1347: yes
262 auto x = 5, *y = &x; // expected-error 0-1{{extension}}
263 auto z = y, *q = y; // expected-error {{'auto' deduced as 'int *' in declaration of 'z' and deduced as 'int' in declaration of 'q'}} expected-error 0-1{{extension}}
264 #if __cplusplus >= 201103L
265 auto a = 5, b = {1, 2}; // expected-error {{'auto' deduced as 'int' in declaration of 'a' and deduced as 'std::initializer_list<int>' in declaration of 'b'}}
266 auto (*fp)(int) -> int, i = 0; // expected-error {{declaration with trailing return type must be the only declaration in its group}}
270 namespace dr1359 { // dr1359: 3.5
271 #if __cplusplus >= 201103L
272 union A { constexpr A() = default; };
273 union B { constexpr B() = default; int a; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
274 union C { constexpr C() = default; int a, b; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
275 struct X { constexpr X() = default; union {}; };
276 struct Y { constexpr Y() = default; union { int a; }; }; // expected-error {{not constexpr}} expected-note 2{{candidate}}
279 constexpr B b = B(); // expected-error {{no matching}}
280 constexpr C c = C(); // expected-error {{no matching}}
282 constexpr Y y = Y(); // expected-error {{no matching}}
286 namespace dr1388 { // dr1388: 4
287 template<typename A, typename ...T> void f(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
288 template<typename ...T> void g(T..., int); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
289 template<typename ...T, typename A> void h(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}}
292 f(0); // ok, trailing parameter pack deduced to empty
293 f(0, 0); // expected-error {{no matching}}
295 f<int>(0, 0); // expected-error {{no matching}}
297 f<int, int, int>(0, 0); // expected-error {{no matching}}
300 g(0, 0); // expected-error {{no matching}}
302 g<int>(0); // expected-error {{no matching}}
306 h(0, 0); // expected-error {{no matching}}
308 h<int, int>(0, 0); // expected-error {{no matching}}
311 // A non-trailing parameter pack is still a non-deduced context, even though
312 // we know exactly how many arguments correspond to it.
313 template<typename T, typename U> struct pair {};
314 template<typename ...T> struct tuple { typedef char type; }; // expected-error 0-2{{C++11}}
315 template<typename ...T, typename ...U> void f_pair_1(pair<T, U>..., int); // expected-error 0-2{{C++11}} expected-note {{different lengths (2 vs. 0)}}
316 template<typename ...T, typename U> void f_pair_2(pair<T, char>..., U); // expected-error 0-2{{C++11}}
317 template<typename ...T, typename ...U> void f_pair_3(pair<T, U>..., tuple<U...>); // expected-error 0-2{{C++11}} expected-note {{different lengths (2 vs. 1)}}
318 template<typename ...T> void f_pair_4(pair<T, char>..., T...); // expected-error 0-2{{C++11}} expected-note {{<int, long> vs. <int, long, const char *>}}
319 void g(pair<int, char> a, pair<long, char> b, tuple<char, char> c) {
320 f_pair_1<int, long>(a, b, 0); // expected-error {{no match}}
321 f_pair_2<int, long>(a, b, 0);
322 f_pair_3<int, long>(a, b, c);
323 f_pair_3<int, long>(a, b, tuple<char>()); // expected-error {{no match}}
324 f_pair_4<int, long>(a, b, 0, 0L);
325 f_pair_4<int, long>(a, b, 0, 0L, "foo"); // expected-error {{no match}}
329 namespace dr1391 { // dr1391: partial
330 struct A {}; struct B : A {};
331 template<typename T> struct C { C(int); typename T::error error; }; // expected-error 2{{'::'}}
332 template<typename T> struct D {};
334 // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match.
335 template<typename T> void a(T, int T::*);
336 void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match
338 namespace dr_example_1 {
339 template<typename T, typename U> void f(C<T>);
340 template<typename T> void f(D<T>);
343 f(d); // ok, first 'f' eliminated by deduction failure
344 f<int>(d); // ok, first 'f' eliminated because 'U' cannot be deduced
348 namespace dr_example_2 {
349 template<typename T> typename C<T>::error f(int, T);
350 template<typename T> T f(T, T);
353 f(a, a); // ok, no conversion from A to int for first parameter of first candidate
357 namespace std_example {
358 template<typename T> struct Z {
359 typedef typename T::x xx;
361 template<typename T> typename Z<T>::xx f(void *, T);
362 template<typename T> void f(int, T);
364 void g() { f(1, a); }
367 template<typename T> void b(C<int> ci, T *p);
370 b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C<int>
371 // FIXME: The "while substituting" note should point at the overload candidate.
372 b<int>(0, 0); // expected-note {{instantiation of}} expected-note {{while substituting}}
375 template<typename T> struct Id { typedef T type; };
376 template<typename T> void c(T, typename Id<C<T> >::type);
378 // Implicit conversion sequences for dependent types are checked later.
379 c(0.0, 0); // expected-note {{instantiation of}}
382 namespace partial_ordering {
383 // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored.
384 template<typename T> int a(T, short) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
385 template<typename T> int a(T*, char); // expected-note {{candidate}}
386 int test_a = a((int*)0, 0); // FIXME: expected-error {{ambiguous}}
388 // FIXME: Second template should be considered more specialized:
389 // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds,
390 // deducing #2 from #1 fails to deduce T, so deduction fails.
391 template<typename T> int b(T, int) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}}
392 template<typename T, typename U> int b(T*, U); // expected-note {{candidate}}
393 int test_b = b((int*)0, 0); // FIXME: expected-error {{ambiguous}}
395 // Unintended consequences: because partial ordering does not consider
396 // explicit template arguments, and deduction from a non-dependent type
397 // vacuously succeeds, a non-dependent template is less specialized than
399 // According to DR1391, this is ambiguous!
400 template<typename T> int c(int);
401 template<typename T> int c(T);
402 int test_c1 = c(0); // ok
403 int test_c2 = c<int>(0); // FIXME: apparently ambiguous
407 namespace dr1399 { // dr1399: dup 1388
408 template<typename ...T> void f(T..., int, T...) {} // expected-note {{candidate}} expected-error 0-1{{C++11}}
412 f(0, 0, 0); // expected-error {{no match}}