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 5 6 __extension__ typedef __SIZE_TYPE__ size_t; 7 8 namespace std { 9 template<typename T> struct initializer_list { 10 const T *ptr; 11 size_t n; 12 initializer_list(const T*, size_t); 13 }; 14 } 15 16 namespace dr1310 { // dr1310: 5 17 struct S {} * sp = new S::S; // expected-error {{qualified reference to 'S' is a constructor name}} f()18 void f() { 19 S::S(a); // expected-error {{qualified reference to 'S' is a constructor name}} 20 } 21 struct T { int n; typedef int U; typedef T V; }; 22 int k = T().T::T::n; 23 T::V v; 24 25 struct U { int U; }; 26 int u = U().U::U; 27 struct U::U w; 28 29 struct V : T::T { 30 // FIXME: This is technically ill-formed, but we consider that to be a defect. Vdr1310::V31 V() : T::T() {} 32 }; 33 template<typename T> struct VT : T::T { VTdr1310::VT34 VT() : T::T() {} 35 }; 36 template struct VT<T>; 37 38 template<template<typename> class> class TT {}; 39 template<typename> class TTy {}; 40 41 template<typename T> struct WBase {}; 42 template<typename T> struct W : WBase<T> { typedef int X; int n; }; 43 w_test()44 void w_test() { 45 W<int>::W w1a; // expected-error {{qualified reference to 'W' is a constructor name}} 46 W<int>::W::X w1ax; 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}} 57 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}} 64 65 W<int> w; 66 (void)w.W::W::n; 67 (void)w.W<int>::W::n; 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}} 70 } 71 72 template<typename W> wt_test()73 void wt_test() { 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}} 78 } 79 template<typename W> wt_test_good()80 void wt_test_good() { 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; 85 86 W w; 87 (void)w.W::W::n; 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; 91 } 92 template void wt_test<W<int> >(); // expected-note {{instantiation of}} 93 template void wt_test_good<W<int> >(); 94 } 95 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> {}; 101 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}} 104 B<1, 2, 3> b1; 105 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; }; 109 B<1, 2, 2>::type b2; 110 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}} 114 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}} 122 } 123 124 namespace dr1330 { // dr1330: 4 c++11 125 // exception-specifications are parsed in a context where the class is complete. 126 struct A { fdr1330::A127 void f() throw(T) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} 128 struct T {}; 129 130 #if __cplusplus >= 201103L gdr1330::A131 void g() noexcept(&a == b) {} 132 static int a; 133 static constexpr int *b = &a; 134 #endif 135 }; 136 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}}}} 139 140 #if __cplusplus >= 201103L 141 static_assert(noexcept(A().g()), ""); 142 #endif 143 144 // Likewise, they're instantiated separately from an enclosing class template. 145 template<typename U> 146 struct B { fdr1330::B147 void f() throw(T, typename U::type) {} // expected-error 0-1{{C++17}} expected-note 0-1{{noexcept}} 148 struct T {}; 149 150 #if __cplusplus >= 201103L gdr1330::B151 void g() noexcept(&a == b && U::value) {} 152 static int a; 153 static constexpr int *b = &a; 154 #endif 155 }; 156 157 B<int> bi; // ok 158 159 struct P { 160 typedef int type; 161 static const int value = true; 162 }; 163 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. 174 #endif 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}} 178 #else 179 // expected-warning@-4 {{not superset}} 180 #endif 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}} 185 #else 186 // expected-error@-4 {{different exception specifications}} 187 #endif 188 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()), ""); 193 #endif 194 f()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}} 198 #endif 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}} 205 #endif 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}} 213 #else 214 // expected-note@-4 {{instantiation of}} 215 #endif 216 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}} 221 #endif 222 }; 223 struct D : C<void> {}; // ok 224 #if __cplusplus < 201103L 225 // expected-note@-2 {{instantiation of}} 226 #endif f(D & d)227 void f(D &d) { d = d; } // ok 228 229 struct E : C<int> {}; // expected-note {{in instantiation of}} 230 #if __cplusplus >= 201103L 231 E e; // expected-note {{needed here}} 232 #endif 233 } 234 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}} 242 #endif f(Ts...ts)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}} 245 } 246 template void f(); // expected-note {{instantiation}} 247 248 #if __cplusplus >= 201103L init_capture()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}} 256 #endif 257 } 258 #endif 259 } 260 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}} 267 #endif 268 } 269 270 namespace dr1358 { // dr1358: yes 271 #if __cplusplus >= 201103L operator intdr1358::Lit272 struct Lit { constexpr operator int() const { return 0; } }; 273 struct NonLit { NonLit(); operator int(); }; // expected-note 2{{no constexpr constructors}} 274 struct NonConstexprConv { constexpr operator int() const; }; 275 struct Virt { virtual int f(int) const; }; 276 277 template<typename T, typename U, typename V> struct A : V { 278 int member; Adr1358::A279 constexpr A(U u) : member(u) {} fdr1358::A280 constexpr T f(U u) const { return T(); } 281 }; 282 283 constexpr A<Lit, Lit, Lit> ce = Lit(); 284 constexpr int k = ce.f(Lit{}); 285 286 // Can have a non-literal return type and parameter type. 287 // Constexpr function can be implicitly virtual. 288 A<NonLit, NonLit, Virt> a = NonLit(); g()289 void g() { a.f(NonLit()); } 290 291 // Constructor is still constexpr, so this is a literal type. 292 static_assert(__is_literal_type(decltype(a)), ""); 293 294 // Constructor can call non-constexpr functions. 295 A<Lit, NonConstexprConv, Lit> b = NonConstexprConv(); 296 297 // But the corresponding non-template cases are rejected. 298 struct B : Virt { 299 int member; Bdr1358::B300 constexpr B(NonLit u) : member(u) {} // expected-error {{not a literal type}} fdr1358::B301 constexpr NonLit f(NonLit u) const { return NonLit(); } // expected-error {{not a literal type}} 302 }; 303 #endif 304 } 305 306 namespace dr1359 { // dr1359: 3.5 307 #if __cplusplus >= 201103L 308 union A { constexpr A() = default; }; 309 union B { constexpr B() = default; int a; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} 310 union C { constexpr C() = default; int a, b; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} 311 struct X { constexpr X() = default; union {}; }; // expected-error {{does not declare anything}} 312 struct Y { constexpr Y() = default; union { int a; }; }; // expected-error {{not constexpr}} expected-note 2{{candidate}} 313 314 constexpr A a = A(); 315 constexpr B b = B(); // expected-error {{no matching}} 316 constexpr C c = C(); // expected-error {{no matching}} 317 constexpr X x = X(); 318 constexpr Y y = Y(); // expected-error {{no matching}} 319 #endif 320 } 321 322 namespace dr1388 { // dr1388: 4 323 template<typename A, typename ...T> void f(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} 324 template<typename ...T> void g(T..., int); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} 325 template<typename ...T, typename A> void h(T..., A); // expected-note 1+{{candidate}} expected-error 0-1{{C++11}} 326 test_f()327 void test_f() { 328 f(0); // ok, trailing parameter pack deduced to empty 329 f(0, 0); // expected-error {{no matching}} 330 f<int>(0); 331 f<int>(0, 0); // expected-error {{no matching}} 332 f<int, int>(0, 0); 333 f<int, int, int>(0, 0); // expected-error {{no matching}} 334 335 g(0); 336 g(0, 0); // expected-error {{no matching}} 337 g<>(0); 338 g<int>(0); // expected-error {{no matching}} 339 g<int>(0, 0); 340 341 h(0); 342 h(0, 0); // expected-error {{no matching}} 343 h<int>(0, 0); 344 h<int, int>(0, 0); // expected-error {{no matching}} 345 } 346 347 // A non-trailing parameter pack is still a non-deduced context, even though 348 // we know exactly how many arguments correspond to it. 349 template<typename T, typename U> struct pair {}; 350 template<typename ...T> struct tuple { typedef char type; }; // expected-error 0-2{{C++11}} 351 template<typename ...T, typename ...U> void f_pair_1(pair<T, U>..., int); // expected-error 0-2{{C++11}} expected-note {{[with T = <int, long>]: deduced incomplete pack <(no value), (no value)> for template parameter 'U'}} 352 template<typename ...T, typename U> void f_pair_2(pair<T, char>..., U); // expected-error 0-2{{C++11}} 353 template<typename ...T, typename ...U> void f_pair_3(pair<T, U>..., tuple<U...>); // expected-error 0-2{{C++11}} expected-note {{deduced packs of different lengths for parameter 'U' (<(no value), (no value)> vs. <char>)}} 354 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 *>}} g(pair<int,char> a,pair<long,char> b,tuple<char,char> c)355 void g(pair<int, char> a, pair<long, char> b, tuple<char, char> c) { 356 f_pair_1<int, long>(a, b, 0); // expected-error {{no match}} 357 f_pair_2<int, long>(a, b, 0); 358 f_pair_3<int, long>(a, b, c); 359 f_pair_3<int, long>(a, b, tuple<char>()); // expected-error {{no match}} 360 f_pair_4<int, long>(a, b, 0, 0L); 361 f_pair_4<int, long>(a, b, 0, 0L, "foo"); // expected-error {{no match}} 362 } 363 } 364 365 namespace dr1391 { // dr1391: partial 366 struct A {}; struct B : A {}; 367 template<typename T> struct C { C(int); typename T::error error; }; // expected-error 2{{'::'}} 368 template<typename T> struct D {}; 369 370 // No deduction is performed for parameters with no deducible template-parameters, therefore types do not need to match. 371 template<typename T> void a(T, int T::*); test_a(int A::* p)372 void test_a(int A::*p) { a(A(), p); } // ok, type of second parameter does not need to match 373 374 namespace dr_example_1 { 375 template<typename T, typename U> void f(C<T>); 376 template<typename T> void f(D<T>); 377 g(D<int> d)378 void g(D<int> d) { 379 f(d); // ok, first 'f' eliminated by deduction failure 380 f<int>(d); // ok, first 'f' eliminated because 'U' cannot be deduced 381 } 382 } 383 384 namespace dr_example_2 { 385 template<typename T> typename C<T>::error f(int, T); 386 template<typename T> T f(T, T); 387 g(A a)388 void g(A a) { 389 f(a, a); // ok, no conversion from A to int for first parameter of first candidate 390 } 391 } 392 393 namespace std_example { 394 template<typename T> struct Z { 395 typedef typename T::x xx; 396 }; 397 template<typename T> typename Z<T>::xx f(void *, T); 398 template<typename T> void f(int, T); 399 struct A {} a; g()400 void g() { f(1, a); } 401 } 402 403 template<typename T> void b(C<int> ci, T *p); 404 void b(...); test_b()405 void test_b() { 406 b(0, 0); // ok, deduction fails prior to forming a conversion sequence and instantiating C<int> 407 // FIXME: The "while substituting" note should point at the overload candidate. 408 b<int>(0, 0); // expected-note {{instantiation of}} expected-note {{while substituting}} 409 } 410 411 template<typename T> struct Id { typedef T type; }; 412 template<typename T> void c(T, typename Id<C<T> >::type); test_c()413 void test_c() { 414 // Implicit conversion sequences for dependent types are checked later. 415 c(0.0, 0); // expected-note {{instantiation of}} 416 } 417 418 namespace partial_ordering { 419 // FIXME: Second template should be considered more specialized because non-dependent parameter is ignored. 420 template<typename T> int a(T, short) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}} 421 template<typename T> int a(T*, char); // expected-note {{candidate}} 422 int test_a = a((int*)0, 0); // FIXME: expected-error {{ambiguous}} 423 424 // FIXME: Second template should be considered more specialized: 425 // deducing #1 from #2 ignores the second P/A pair, so deduction succeeds, 426 // deducing #2 from #1 fails to deduce T, so deduction fails. 427 template<typename T> int b(T, int) = delete; // expected-error 0-1{{extension}} expected-note {{candidate}} 428 template<typename T, typename U> int b(T*, U); // expected-note {{candidate}} 429 int test_b = b((int*)0, 0); // FIXME: expected-error {{ambiguous}} 430 431 // Unintended consequences: because partial ordering does not consider 432 // explicit template arguments, and deduction from a non-dependent type 433 // vacuously succeeds, a non-dependent template is less specialized than 434 // anything else! 435 // According to DR1391, this is ambiguous! 436 template<typename T> int c(int); 437 template<typename T> int c(T); 438 int test_c1 = c(0); // ok 439 int test_c2 = c<int>(0); // FIXME: apparently ambiguous 440 } 441 } 442 443 namespace dr1399 { // dr1399: dup 1388 f(T...,int,T...)444 template<typename ...T> void f(T..., int, T...) {} // expected-note {{candidate}} expected-error 0-1{{C++11}} g()445 void g() { 446 f(0); 447 f<int>(0, 0, 0); 448 f(0, 0, 0); // expected-error {{no match}} 449 } 450 } 451