1318 lines · cpp
1// RUN: %clang_cc1 -std=c++98 %s -verify=expected,cxx98-23,cxx98-11,cxx98-14,cxx98-17,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors2// RUN: %clang_cc1 -std=c++11 %s -verify=expected,cxx98-23,cxx98-11,cxx98-14,cxx98-17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors3// RUN: %clang_cc1 -std=c++14 %s -verify=expected,cxx98-23,cxx98-14,cxx98-17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors4// RUN: %clang_cc1 -std=c++17 %s -verify=expected,cxx98-23,since-cxx17,cxx98-17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors5// RUN: %clang_cc1 -std=c++20 %s -verify=expected,cxx98-23,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors6// RUN: %clang_cc1 -std=c++23 %s -verify=expected,cxx98-23,since-cxx23,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors7// RUN: %clang_cc1 -std=c++2c %s -verify=expected,since-cxx26,since-cxx23,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors8 9#if __cplusplus == 199711L10#define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)11// cxx98-error@-1 {{variadic macros are a C99 feature}}12#endif13 14// FIXME: This is included to avoid a diagnostic with no source location15// pointing at the implicit operator new. We can't match such a diagnostic16// with -verify.17__extension__ typedef __SIZE_TYPE__ size_t;18void *operator new(size_t); // #cwg5xx-global-operator-new19// cxx98-error@-1 {{'operator new' is missing exception specification 'throw(std::bad_alloc)'}}20#if __cplusplus >= 201703L21namespace std {22 enum class align_val_t : size_t {};23} // namespace std24void *operator new(size_t, std::align_val_t); // #cwg5xx-global-operator-new-aligned25#endif26 27namespace std {28 struct type_info;29} // namespace std30 31namespace cwg500 { // cwg500: dup 37232 class D;33 class A {34 class B;35 class C;36 friend class D;37 };38 class A::B {};39 class A::C : public A::B {};40 class D : public A::B {};41} // namespace cwg50042 43namespace cwg501 { // cwg501: 2.744 struct A {45 friend void f() {}46 void g() {47 void (*p)() = &f;48 // expected-error@-1 {{use of undeclared identifier 'f'}}49 }50 };51} // namespace cwg50152 53namespace cwg502 { // cwg502: 2.754 struct Q {};55 template<typename T> struct A {56 enum E { e = 1 };57 void q1() { f(e); }58 void q2() { Q arr[sizeof(E)]; f(arr); }59 void q3() { Q arr[e]; f(arr); }60 void sanity() { Q arr[1]; f(arr); }61 // expected-error@-1 {{use of undeclared identifier 'f'}}62 };63 int f(A<int>::E);64 template<int N> int f(Q (&)[N]);65 template struct A<int>;66} // namespace cwg50267 68namespace cwg505 { // cwg505: 2.769 const char *exts = "\e\(\{\[\%";70 // expected-error@-1 {{use of non-standard escape character '\e'}}71 // expected-error@-2 {{use of non-standard escape character '\('}}72 // expected-error@-3 {{use of non-standard escape character '\{'}}73 // expected-error@-4 {{use of non-standard escape character '\['}}74 // expected-error@-5 {{use of non-standard escape character '\%'}}75 const char *unknown = "\Q";76 // expected-error@-1 {{unknown escape sequence '\Q'}}77} // namespace cwg50578 79namespace cwg506 { // cwg506: 2.780 struct NonPod { ~NonPod(); };81 void f(...);82 void g(NonPod np) { f(np); }83 // cxx98-error@-1 {{cannot pass object of non-POD type 'NonPod' through variadic function; call will abort at runtime}}84 // since-cxx11-error@-2 {{cannot pass object of non-trivial type 'NonPod' through variadic function; call will abort at runtime}}85} // namespace cwg50686 87// FIXME: Add tests here once CWG260 is resolved.88// cwg507: dup 26089 90// cwg508: na91// cwg509: na92// cwg510: na93 94namespace cwg512 { // cwg512: 3.095 struct A { // #cwg512-A96 A(int); // #cwg512-A-ctor97 };98 union U { A a; };99 // cxx98-error@-1 {{union member 'a' has a non-trivial default constructor}}100 // cxx98-note@#cwg512-A {{because type 'cwg512::A' has no default constructor}}101 // cxx98-note@#cwg512-A-ctor {{implicit default constructor suppressed by user-declared constructor}}102} // namespace cwg512103 104// cwg513: na105 106namespace cwg514 { // cwg514: 2.7107 namespace A { extern int x, y; }108 int A::x = y;109} // namespace cwg514110 111namespace cwg515 { // cwg515: sup 1017112 // FIXME: cwg1017 reverses the wording of cwg515, but the current draft has113 // cwg515's wording, with a different fix for cwg1017.114 115 struct X { int n; };116 template<typename T> struct Y : T {117 int f() { return X::n; }118 };119 int k = Y<X>().f();120 121 struct A { int a; };122 struct B { void f() { int k = sizeof(A::a); } };123 // cxx98-error@-1 {{invalid use of non-static data member 'a'}}124} // namespace cwg515125 126// cwg516: na127 128namespace cwg517 { // cwg517: no129 // This is NDR, but we should diagnose it anyway.130 template<typename T> struct S {};131 template<typename T> int v = 0;132 // cxx98-11-error@-1 {{variable templates are a C++14 extension}}133 134 template struct S<int*>;135 template int v<int*>;136 137 S<char&> s;138 int k = v<char&>;139 140 // FIXME: These are both ill-formed.141 template<typename T> struct S<T*> {};142 template<typename T> int v<T*> = 0;143 144 // FIXME: These are both ill-formed.145 template<typename T> struct S<T&> {};146 template<typename T> int v<T&> = 0;147} // namespace cwg517148 149namespace cwg518 { // cwg518: 2.7 c++11150 enum E { e, };151 // cxx98-error@-1 {{commas at the end of enumerator lists are a C++11 extension}}152} // namespace cwg518153 154// cwg519 is in cwg519.cpp155// cwg520: na156 157// cwg521: no158// FIXME: The wording here is broken. It's not reasonable to expect a159// diagnostic here. Once the relevant DR gets a number, mark this as a dup.160 161namespace cwg522 { // cwg522: 2.7162 struct S {};163 template<typename T> void b1(volatile T &);164 template<typename T> void b2(volatile T * const *);165 template<typename T> void b2(volatile T * const S::*);166 template<typename T> void b2(volatile T * const S::* const *);167 template<typename T> void b2a(volatile T *S::* const *); // #cwg522-b2a168 169 template<typename T> struct Base {};170 struct Derived : Base<int> {};171 template<typename T> void b3(Base<T>);172 template<typename T> void b3(Base<T> *);173 174 void test(int n, const int cn, int **p, int *S::*pm) {175 int *a[3], *S::*am[3];176 const Derived cd = Derived();177 Derived d[3];178 179 b1(n);180 b1(cn);181 b2(p);182 b2(pm);183 b2(a);184 b2(am);185 b2a(am);186 // expected-error@-1 {{no matching function for call to 'b2a'}}187 // expected-note@#cwg522-b2a {{candidate template ignored: deduced type 'volatile int *S::*const *' of 1st parameter does not match adjusted type 'int *S::**' of argument}}188 b3(d);189 b3(cd);190 }191} // namespace cwg522192 193namespace cwg524 { // cwg524: 2.7194 template<typename T> void f(T a, T b) { operator+(a, b); }195 // expected-error@-1 {{call to function 'operator+' that is neither visible in the template definition nor found by argument-dependent lookup}}196 // expected-note@#cwg524-f-N-S {{in instantiation of function template specialization 'cwg524::f<cwg524::N::S>' requested here}}197 // expected-note@#cwg524-operator-plus {{'operator+' should be declared prior to the call site or in namespace 'cwg524::N'}}198 199 struct S {};200 void operator+(S, S);201 template void f(S, S);202 203 namespace N { struct S {}; }204 void operator+(N::S, N::S); // #cwg524-operator-plus205 template void f(N::S, N::S); // #cwg524-f-N-S206} // namespace cwg524207 208namespace cwg525 { // cwg525: 2.7209 namespace before {210 // Note, the example was correct prior to the change; instantiation is211 // required for cases like this:212 template <class T> struct D { operator T*(); };213 void g(D<double> ppp) {214 delete ppp;215 }216 }217 namespace after {218 template <class T> struct D { typename T::error e; };219 // expected-error@-1 {{type 'double' cannot be used prior to '::' because it has no members}}220 // expected-note@#cwg525-ppp {{in instantiation of template class 'cwg525::after::D<double>' requested here}}221 void g(D<double> *ppp) {222 delete ppp; // #cwg525-ppp223 }224 }225} // namespace cwg525226 227namespace cwg526 { // cwg526: 2.7228 template<int> struct S {};229 template<int N> void f1(S<N> s);230 template<int N> void f2(S<(N)> s); // #cwg526-f2231 template<int N> void f3(S<+N> s); // #cwg526-f3232 template<int N> void g1(int (&)[N]);233 template<int N> void g2(int (&)[(N)]); // #cwg526-g2234 template<int N> void g3(int (&)[+N]); // #cwg526-g3235 236 void test(int (&a)[3], S<3> s) {237 f1(s);238 f2(s);239 // expected-error@-1 {{no matching function for call to 'f2'}}240 // expected-note@#cwg526-f2 {{candidate template ignored: couldn't infer template argument 'N'}}241 f3(s);242 // expected-error@-1 {{no matching function for call to 'f3'}}243 // expected-note@#cwg526-f3 {{candidate template ignored: couldn't infer template argument 'N'}}244 g1(a);245 g2(a);246 // expected-error@-1 {{no matching function for call to 'g2'}}247 // expected-note@#cwg526-g2 {{candidate template ignored: couldn't infer template argument 'N'}}248 g3(a);249 // expected-error@-1 {{no matching function for call to 'g3'}}250 // expected-note@#cwg526-g3 {{candidate template ignored: couldn't infer template argument 'N'}}251 }252 253 template<int N> struct X {254 typedef int type;255 X<N>::type v1;256 X<(N)>::type v2;257 // cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<(N)>::type' is a C++20 extension}}258 X<+N>::type v3;259 // cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'X<+N>::type' is a C++20 extension}}260 };261} // namespace cwg526262 263namespace cwg527 { // cwg527: na264 // This DR is meaningless. It removes a required diagnostic from the case265 // where a not-externally-visible object is odr-used but not defined, which266 // requires a diagnostic for a different reason.267 extern struct { int x; } a; // FIXME: We should reject this, per cwg389.268 static struct { int x; } b;269 extern "C" struct { int x; } c;270 namespace { extern struct { int x; } d; }271 typedef struct { int x; } *P;272 struct E { static P e; }; // FIXME: We should reject this, per cwg389.273 namespace { struct F { static P f; }; }274 275 int ax = a.x, bx = b.x, cx = c.x, dx = d.x, ex = E::e->x, fx = F::f->x;276} // namespace cwg527277 278namespace cwg528 { // cwg528: 2.7279 280struct S; // #cwg528-S281 282void f() {283 typeid(S);284 // expected-error@-1 {{'typeid' of incomplete type 'S'}}285 // expected-note@#cwg528-S {{forward declaration of 'cwg528::S'}}286}287 288} // namespace cwg528289 290namespace cwg530 { // cwg530: 2.7291 template<int*> struct S { enum { N = 1 }; };292 template<void(*)()> struct T { enum { N = 1 }; };293 int n;294 void f();295 int a[S<&n>::N];296 int b[T<&f>::N];297} // namespace cwg530298 299namespace cwg531 { // cwg531: partial300 namespace good {301 template<typename T> struct A {302 void f(T) { T::error; }303 template<typename U> void g(T, U) { T::error; }304 struct B { typename T::error error; };305 template<typename U> struct C { typename T::error error; };306 static T n;307 };308 template<typename T> T A<T>::n = T::error;309 310 template<> void A<int>::f(int) {}311 template<> template<typename U> void A<int>::g(int, U) {}312 template<> struct A<int>::B {};313 template<> template<typename U> struct A<int>::C {};314 template<> int A<int>::n = 0;315 316 void use(A<int> a) {317 a.f(a.n);318 a.g(0, 0);319 A<int>::B b;320 A<int>::C<int> c;321 }322 323 template<> struct A<char> {324 void f(char);325 template<typename U> void g(char, U);326 struct B;327 template<typename U> struct C;328 static char n;329 };330 331 void A<char>::f(char) {}332 template<typename U> void A<char>::g(char, U) {}333 struct A<char>::B {};334 template<typename U> struct A<char>::C {};335 char A<char>::n = 0;336 }337 338 namespace bad {339 template<typename T> struct A {340 void f(T) { T::error; }341 template<typename U> void g(T, U) { T::error; }342 struct B { typename T::error error; };343 template<typename U> struct C { typename T::error error; }; // #cwg531-C344 static T n;345 };346 template<typename T> T A<T>::n = T::error;347 348 void A<int>::f(int) {}349 // expected-error@-1 {{template specialization requires 'template<>'}}350 template<typename U> void A<int>::g(int, U) {}351 // expected-error@-1 {{template parameter list matching the non-templated nested type 'cwg531::bad::A<int>' should be empty}}352 struct A<int>::B {};353 // expected-error@-1 {{template specialization requires 'template<>'}}354 template<typename U> struct A<int>::C {};355 // expected-error@-1 {{template parameter list matching the non-templated nested type 'cwg531::bad::A<int>' should be empty}}356 // expected-error@-2 {{redefinition of 'C' as different kind of symbol}}357 // expected-note@#cwg531-C {{previous definition is here}}358 int A<int>::n = 0;359 // expected-error@-1 {{template specialization requires 'template<>'}}360 361 template<> struct A<char> { // #cwg531-A-char362 void f(char);363 template<typename U> void g(char, U);364 struct B; // #cwg531-B365 template<typename U> struct C;366 static char n;367 };368 369 template<> void A<char>::f(char) {}370 // expected-error@-1 {{no function template matches function template specialization 'f'}}371 template<> template<typename U> void A<char>::g(char, U) {}372 // expected-error@-1 {{extraneous template parameter list in template specialization}}373 // expected-note@#cwg531-A-char {{'template<>' header not required for explicitly-specialized class 'cwg531::bad::A<char>' declared here}}374 template<> struct A<char>::B {};375 // expected-error@-1 {{extraneous 'template<>' in declaration of struct 'B'}}376 // expected-error@-2 {{specialization of member 'cwg531::bad::A<char>::B' does not specialize an instantiated member}}377 // expected-note@#cwg531-B {{attempt to specialize declaration here}}378 template<> template<typename U> struct A<char>::C {};379 // expected-error@-1 {{extraneous template parameter list in template specialization}}380 // expected-note@#cwg531-A-char {{'template<>' header not required for explicitly-specialized class 'cwg531::bad::A<char>' declared here}}381 template<> char A<char>::n = 0;382 // expected-error@-1 {{extraneous 'template<>' in declaration of variable 'n'}}383 }384 385 namespace nested {386 template<typename T> struct A {387 template<typename U> struct B;388 };389 template<> template<typename U> struct A<int>::B {390 void f();391 void g();392 template<typename V> void h();393 template<typename V> void i();394 };395 template<> template<typename U> void A<int>::B<U>::f() {}396 template<typename U> void A<int>::B<U>::g() {}397 // expected-error@-1 {{template parameter list matching the non-templated nested type 'cwg531::nested::A<int>' should be empty ('template<>')}}398 399 template<> template<typename U> template<typename V> void A<int>::B<U>::h() {}400 template<typename U> template<typename V> void A<int>::B<U>::i() {}401 // expected-error@-1 {{template parameter list matching the non-templated nested type 'cwg531::nested::A<int>' should be empty ('template<>')}}402 403#if __cplusplus <= 201703L404 // FIXME: All of those declarations shouldn't crash in C++20 mode.405 template<> template<> void A<int>::B<int>::f() {}406 template<> template<> template<typename V> void A<int>::B<int>::h() {}407 template<> template<> template<> void A<int>::B<int>::h<int>() {}408 409 template<> void A<int>::B<char>::f() {}410 // cxx98-17-error@-1 {{template specialization requires 'template<>'}}411 template<> template<typename V> void A<int>::B<char>::h() {}412 // cxx98-17-error@-1 {{template parameter list matching the non-templated nested type 'cwg531::nested::A<int>::B<char>' should be empty ('template<>')}}413#endif414 }415} // namespace cwg531416 417// PR8130418namespace cwg532 { // cwg532: 3.5419 struct A { };420 421 template<class T> struct B {422 template<class R> int &operator*(R&);423 };424 425 template<class T, class R> float &operator*(T&, R&);426 void test() {427 A a;428 B<A> b;429 int &ir = b * a;430 }431} // namespace cwg532432 433// cwg533: na434 435namespace cwg534 { // cwg534: 2.9436 struct S {};437 template<typename T> void operator+(S, T);438 template<typename T> void operator+<T*>(S, T*) {}439 // expected-error@-1 {{function template partial specialization is not allowed}}440} // namespace cwg534441 442namespace cwg535 { // cwg535: 3.1443 class X { private: X(const X&); };444 struct A {445 X x;446 template<typename T> A(T&);447 };448 struct B : A {449 X y;450 B(volatile A&);451 };452 453 extern A a1;454 A a2(a1); // ok, uses constructor template455 456 extern volatile B b1;457 B b2(b1); // ok, uses converting constructor458 459 void f() { throw a1; }460 461#if __cplusplus >= 201103L462 struct C {463 constexpr C() : n(0) {}464 template<typename T> constexpr C(T&t) : n(t.n == 0 ? throw 0 : 0) {}465 int n;466 };467 constexpr C c() { return C(); }468 // ok, copy is elided469 constexpr C x = c();470#endif471} // namespace cwg535472 473// cwg536: na474// cwg537: na475// cwg538: na476 477namespace cwg539 { // cwg539: 3.4478const f(479// expected-error@-1 {{a type specifier is required for all declarations}}480 const a) {481 // expected-error@-1 {{unknown type name 'a'}}482 const b;483 // expected-error@-1 {{a type specifier is required for all declarations}}484 new const;485 // expected-error@-1 {{expected a type}}486 try {} catch (const n) {}487 // expected-error@-1 {{unknown type name 'n'}}488 try {} catch (const) {}489 // expected-error@-1 {{expected a type}}490 if (const n = 0) {}491 // expected-error@-1 {{a type specifier is required for all declarations}}492 switch (const n = 0) {}493 // expected-error@-1 {{a type specifier is required for all declarations}}494 while (const n = 0) {}495 // expected-error@-1 {{a type specifier is required for all declarations}}496 for (const n = 0;497 // expected-error@-1 {{a type specifier is required for all declarations}}498 const m = 0; ) {}499 // expected-error@-1 {{a type specifier is required for all declarations}}500 sizeof(const);501 // expected-error@-1 {{a type specifier is required for all declarations}}502 struct S {503 const n;504 // expected-error@-1 {{a type specifier is required for all declarations}}505 operator const();506 // expected-error@-1 {{expected a type}}507 };508#if __cplusplus >= 201103L509 int arr[3];510 // FIXME: The extra braces here are to avoid the parser getting too511 // badly confused when recovering here. We should fix this recovery.512 { for (const n // #cwg539-for513 // since-cxx11-error@-1 {{unknown type name 'n'}}514 : arr) ; {} }515 // since-cxx11-error@-1 +{{}}516 // since-cxx11-note@#cwg539-for {{}}517 (void) [](const) {};518 // since-cxx11-error@-1 {{a type specifier is required for all declarations}}519 (void) [](const n) {};520 // since-cxx11-error@-1 {{unknown type name 'n'}}521 enum E : const {};522 // since-cxx11-error@-1 {{expected a type}}523 using T = const;524 // since-cxx11-error@-1 {{expected a type}}525 auto f() -> const;526 // since-cxx11-error@-1 {{expected a type}}527#endif528}529} // namespace cwg539530 531namespace cwg540 { // cwg540: 2.7532 typedef int &a;533 typedef const a &a;534 // expected-warning@-1 {{'const' qualifier on reference type 'a' (aka 'int &') has no effect}}535 typedef const int &b;536 typedef b &b;537 typedef const a &c; // #cwg540-typedef-a-c538 // expected-warning@-1 {{'const' qualifier on reference type 'a' (aka 'int &') has no effect}}539 typedef const b &c; // #cwg540-typedef-b-c540 // expected-error@#cwg540-typedef-b-c {{typedef redefinition with different types ('const int &' vs 'int &')}}541 // expected-note@#cwg540-typedef-a-c {{previous definition is here}}542 // expected-warning@#cwg540-typedef-b-c {{'const' qualifier on reference type 'b' (aka 'const int &') has no effect}}543} // namespace cwg540544 545namespace cwg541 { // cwg541: 2.7546 template<int> struct X { typedef int type; };547 template<typename T> struct S {548 int f(T);549 550 int g(int);551 T g(bool);552 553 int h();554 int h(T);555 556 void x() {557 // These are type-dependent expressions, even though we could558 // determine that all calls have type 'int'.559 X<sizeof(f(0))>::type a;560 // expected-error@-1 {{expected ';' after expression}}561 // expected-error@-2 {{use of undeclared identifier 'a'}}562 X<sizeof(g(0))>::type b;563 // expected-error@-1 {{expected ';' after expression}}564 // expected-error@-2 {{use of undeclared identifier 'b'}}565 X<sizeof(h(0))>::type b;566 // expected-error@-1 {{expected ';' after expression}}567 // expected-error@-2 {{use of undeclared identifier 'b'}}568 569 typename X<sizeof(f(0))>::type a;570 typename X<sizeof(h(0))>::type b;571 }572 };573} // namespace cwg541574 575namespace cwg542 { // cwg542: 3.5576#if __cplusplus >= 201103L577 // In C++20 A and B are no longer aggregates and thus the constructor is578 // called, which fails.579 struct A { A() = delete; int n; }; // #cwg542-A580 // ok, constructor not called581 A a[32] = {}; // #cwg542-a582 // since-cxx20-error@-1 {{call to deleted constructor of 'A'}}583 // since-cxx20-note@#cwg542-A {{'A' has been explicitly marked deleted here}}584 // since-cxx20-note@#cwg542-a {{in implicit initialization of array element 0 with omitted initializer}}585 586 struct B {587 int n;588 private:589 B() = default; // #cwg542-B-ctor590 };591 B b[32] = {}; // ok, constructor not called592 // since-cxx20-error@-1 {{calling a private constructor of class 'cwg542::B'}}593 // since-cxx20-note@#cwg542-B-ctor {{declared private here}}594#endif595} // namespace cwg542596 597namespace cwg543 { // cwg543: 3.0598 // In C++98+CWG543, this is valid because value-initialization doesn't call a599 // trivial default constructor, so we never notice that defining the600 // constructor would be ill-formed.601 //602 // In C++11+CWG543, this is ill-formed, because the default constructor is603 // deleted, and value-initialization *does* call a deleted default604 // constructor, even if it is trivial.605 struct A {606 const int n; // #cwg543-A-n607 };608 A a = A();609 // since-cxx11-error@-1 {{call to implicitly-deleted default constructor of 'A'}}610 // since-cxx11-note@#cwg543-A-n {{default constructor of 'A' is implicitly deleted because field 'n' of const-qualified type 'const int' would not be initialized}}611} // namespace cwg543612 613namespace cwg544 { // cwg544: 2.7614 int *n;615 616 template<class T> struct A { int n; };617 template<class T> struct B : A<T> { int get(); };618 template<> int B<int>::get() { return n; }619 int k = B<int>().get();620} // namespace cwg544621 622namespace cwg546 { // cwg546: 2.7623 template<typename T> struct A { void f(); };624 template struct A<int>;625 template<typename T> void A<T>::f() { T::error; }626} // namespace cwg546627 628namespace cwg547 { // cwg547: 3.2629 template<typename T> struct X;630 template<typename T> struct X<T() const> {};631 template<typename T, typename C> X<T> f(T C::*) { return X<T>(); }632 633 struct S { void f() const; };634 X<void() const> x = f(&S::f);635} // namespace cwg547636 637namespace cwg548 { // cwg548: dup 482638 template<typename T> struct S {};639 template<typename T> void f() {}640 template struct cwg548::S<int>;641 template void cwg548::f<int>();642} // namespace cwg548643 644// cwg550: dup 393645 646namespace cwg551 { // cwg551: 2.7 c++11647 // FIXME: This obviously should apply in C++98 mode too.648 template<typename T> void f() {}649 template inline void f<int>();650 // since-cxx11-error@-1 {{explicit instantiation cannot be 'inline'}}651 652 template<typename T> inline void g() {}653 template inline void g<int>();654 // since-cxx11-error@-1 {{explicit instantiation cannot be 'inline'}}655 656 template<typename T> struct X {657 void f() {}658 };659 template inline void X<int>::f();660 // since-cxx11-error@-1 {{explicit instantiation cannot be 'inline'}}661} // namespace cwg551662 663namespace cwg552 { // cwg552: 2.7664 template<typename T, typename T::U> struct X {};665 struct Y { typedef int U; };666 X<Y, 0> x;667} // namespace cwg552668 669// cwg553: 2.7670struct cwg553_class {671 friend void *operator new(size_t, cwg553_class);672};673namespace cwg553 {674 cwg553_class c;675 // Contrary to the apparent intention of the DR, operator new is not actually676 // looked up with a lookup mechanism that performs ADL; the standard says it677 // "is looked up in global scope", where it is not visible.678 void *p = new (c) int;679 // expected-error@-1 {{no matching function for call to 'operator new'}}680 // since-cxx17-note@#cwg5xx-global-operator-new-aligned {{candidate function not viable: no known conversion from 'cwg553_class' to 'std::align_val_t' for 2nd argument}}681 // expected-note@#cwg5xx-global-operator-new {{candidate function not viable: requires 1 argument, but 2 were provided}}682 683 struct namespace_scope {684 friend void *operator new(size_t, namespace_scope);685 // expected-error@-1 {{'operator new' cannot be declared inside a namespace}}686 };687} // namespace cwg553688 689// cwg554: na690 691namespace cwg555 { // cwg555: 2.8692typedef int I;693typedef const int CI;694typedef volatile int VI;695void f(int *a, CI *b, VI *c) {696 a->~I();697 a->~CI();698 a->~VI();699 a->I::~I();700 a->CI::~CI();701 a->VI::~VI();702 703 a->CI::~VI(); // allowed by changes to [expr.id.prim.qual]/2 introduced in P1131R2704 705 b->~I();706 b->~CI();707 b->~VI();708 b->I::~I();709 b->CI::~CI();710 b->VI::~VI();711 712 c->~I();713 c->~CI();714 c->~VI();715 c->I::~I();716 c->CI::~CI();717 c->VI::~VI();718}719 720void g(int &a, CI &b, VI &c) {721 a.~I();722 a.~CI();723 a.~VI();724 a.I::~I();725 a.CI::~CI();726 a.VI::~VI();727 728 a.CI::~VI(); // allowed by changes to [expr.id.prim.qual]/2 introduced in P1131R2729 730 b.~I();731 b.~CI();732 b.~VI();733 b.I::~I();734 b.CI::~CI();735 b.VI::~VI();736 737 c.~I();738 c.~CI();739 c.~VI();740 c.I::~I();741 c.CI::~CI();742 c.VI::~VI();743}744} // namespace cwg555745 746// cwg556: na747 748namespace cwg557 { // cwg557: 3.1749 template<typename T> struct S {750 friend void f(S<T> *);751 friend void g(S<S<T> > *);752 };753 void x(S<int> *p, S<S<int> > *q) {754 f(p);755 g(q);756 }757} // namespace cwg557758 759namespace cwg558 { // cwg558: 2.9760 wchar_t a = L'\uD7FF';761 wchar_t b = L'\xD7FF';762 wchar_t c = L'\uD800';763 // expected-error@-1 {{invalid universal character}}764 wchar_t d = L'\xD800';765 wchar_t e = L'\uDFFF';766 // expected-error@-1 {{invalid universal character}}767 wchar_t f = L'\xDFFF';768 wchar_t g = L'\uE000';769 wchar_t h = L'\xE000';770} // namespace cwg558771 772namespace cwg559 { // cwg559: 2.7773template<typename> struct S { typedef int T; S::T u; };774} // namespace cwg559775 776namespace cwg560 { // cwg560: 16777 778template <class T>779struct Outer {780 struct Inner {781 Inner* self();782 };783};784template <class T>785Outer<T>::Inner* Outer<T>::Inner::self() { return this; }786// cxx98-17-error@-1 {{missing 'typename' prior to dependent type name 'Outer<T>::Inner' is a C++20 extension}}787 788} // namespace cwg560789 790namespace cwg561 { // cwg561: 2.7791 template<typename T> void f(int);792 template<typename T> void g(T t) {793 f<T>(t);794 }795 namespace {796 struct S {};797 template<typename T> static void f(S);798 }799 void h(S s) {800 g(s);801 }802} // namespace cwg561803 804// cwg562: na805// cwg563 is in cwg563.cpp806 807namespace cwg564 { // cwg564: 2.7808 extern "C++" void f(int);809 void f(int); // ok810 extern "C++" { extern int n; }811 int n; // ok812} // namespace cwg564813 814namespace cwg565 { // cwg565: 2.7815 namespace N {816 template<typename T> int f(T); // #cwg565-f817 }818 using N::f; // #cwg565-using819 template<typename T> int f(T*);820 template<typename T> void f(T);821 template<typename T, int = 0> int f(T);822 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}823 template<typename T> int f(T, int = 0);824 template<typename T> int f(T);825 // expected-error@-1 {{declaration conflicts with target of using declaration already in scope}}826 // expected-note@#cwg565-f {{target of using declaration}}827 // expected-note@#cwg565-using {{using declaration}}828} // namespace cwg565829 830namespace cwg566 { // cwg566: 3.1831#if __cplusplus >= 201103L832 static_assert(int(-3.99) == -3, "");833#endif834} // namespace cwg566835 836// cwg567: na837 838namespace cwg568 { // cwg568: 3.0 c++11839 // FIXME: This is a DR issue against C++98, so should probably apply there840 // too.841 struct x { int y; };842 class trivial : x {843 x y;844 public:845 int n;846 };847 static_assert(__is_trivial(trivial), "");848 849 struct std_layout {850 std_layout();851 std_layout(const std_layout &);852 ~std_layout();853 private:854 int n;855 };856 static_assert(__is_standard_layout(std_layout), "");857 858 struct aggregate {859 int x;860 int y;861 trivial t;862 std_layout sl;863 };864 aggregate aggr = {};865 866 void f(...);867 void g(trivial t) { f(t); }868 // cxx98-error@-1 {{cannot pass object of non-POD type 'trivial' through variadic function; call will abort at runtime}}869 870 void jump() {871 goto x;872 // cxx98-error@-1 {{cannot jump from this goto statement to its label}}873 // cxx98-note@#cwg568-t {{jump bypasses initialization of non-POD variable}}874 trivial t; // #cwg568-t875 x: ;876 }877} // namespace cwg568878 879namespace cwg569 { // cwg569: 2.7 c++11880 // FIXME: This is a DR issue against C++98, so should probably apply there881 // too.882 ;;;;;883 // cxx98-error@-1 {{C++11 extension}}884} // namespace cwg569885 886namespace cwg570 { // cwg570: dup 633887 int n;888 int &r = n; // #cwg570-r889 int &r = n;890 // expected-error@-1 {{redefinition of 'r'}}891 // expected-note@#cwg570-r {{previous definition is here}}892} // namespace cwg570893 894// cwg571 is in cwg571.cpp895 896namespace cwg572 { // cwg572: 2.7897 enum E { a = 1, b = 2 };898 static_assert(a + b == 3, "");899} // namespace cwg572900 901namespace cwg573 { // cwg573: no902 void *a;903 int *b = reinterpret_cast<int*>(a);904 void (*c)() = reinterpret_cast<void(*)()>(a);905 // cxx98-error@-1 {{cast between pointer-to-function and pointer-to-object is an extension}}906 void *d = reinterpret_cast<void*>(c);907 // cxx98-error@-1 {{cast between pointer-to-function and pointer-to-object is an extension}}908 void f() { delete a; }909 // cxx98-23-error@-1 {{cannot delete expression with pointer-to-'void' type 'void *'}}910 // since-cxx26-error@-2 {{cannot delete pointer to incomplete type 'void'}}911 int n = d - a;912 // expected-error@-1 {{arithmetic on pointers to void}}913 // FIXME: This is ill-formed.914 template<void*> struct S;915 template<int*> struct T;916} // namespace cwg573917 918namespace cwg574 { // cwg574: 3.0919 struct A {920 A &operator=(const A&) const; // #cwg574-A-copy-assign921 };922 struct B {923 B &operator=(const B&) volatile; // #cwg574-B-copy-assign924 };925#if __cplusplus >= 201103L926 struct C {927 C &operator=(const C&) &; // #cwg574-C-copy-assign928 };929 struct D {930 D &operator=(const D&) &&; // #cwg574-D-copy-assign931 };932 void test(C c, D d) {933 c = c;934 C() = c;935 // since-cxx11-error@-1 {{no viable overloaded '='}}936 // since-cxx11-note@#cwg574-C-copy-assign {{candidate function not viable: expects an lvalue for object argument}}937 d = d;938 // since-cxx11-error@-1 {{no viable overloaded '='}}939 // since-cxx11-note@#cwg574-D-copy-assign {{candidate function not viable: expects an rvalue for object argument}}940 D() = d;941 }942#endif943 struct Test {944 friend A &A::operator=(const A&);945 // expected-error@-1 {{friend declaration of 'operator=' does not match any declaration in 'cwg574::A'}}946 // expected-note@#cwg574-A-copy-assign {{candidate function has different qualifiers (expected unqualified but found 'const')}}947 friend B &B::operator=(const B&);948 // expected-error@-1 {{friend declaration of 'operator=' does not match any declaration in 'cwg574::B'}}949 // expected-note@#cwg574-B-copy-assign {{candidate function has different qualifiers (expected unqualified but found 'volatile')}}950#if __cplusplus >= 202302L951 friend C &C::operator=(const C&);952 // since-cxx23-error@-1 {{conflicting types for 'operator='}}953 // since-cxx23-note@#cwg574-C-copy-assign {{previous declaration is here}}954 friend D &D::operator=(const D&);955 // since-cxx23-error@-1 {{conflicting types for 'operator='}}956 // since-cxx23-note@#cwg574-D-copy-assign {{previous declaration is here}}957#elif __cplusplus >= 201103L958 // FIXME: We shouldn't produce the 'cannot overload' diagnostics here.959 friend C &C::operator=(const C&); // #cwg574-test-C960 // since-cxx11-error@#cwg574-test-C {{cannot overload}}961 // since-cxx11-note@#cwg574-C-copy-assign {{previous declaration is here}}962 // since-cxx11-error@#cwg574-test-C {{friend declaration of 'operator=' does not match any declaration in 'cwg574::C'}}963 // since-cxx11-note@#cwg574-C-copy-assign {{candidate function}}964 friend D &D::operator=(const D&); // #cwg574-test-D965 // since-cxx11-error@#cwg574-test-D {{cannot overload a member function without a ref-qualifier with a member function with ref-qualifier '&&'}}966 // since-cxx11-note@#cwg574-D-copy-assign {{previous declaration is here}}967 // since-cxx11-error@#cwg574-test-D {{friend declaration of 'operator=' does not match any declaration in 'cwg574::D'}}968 // since-cxx11-note@#cwg574-D-copy-assign {{candidate function}}969#endif970 };971} // namespace cwg574972 973namespace cwg575 { // cwg575: 2.7974 template<typename T, typename U = typename T::type> void a(T); void a(...);975 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}976 template<typename T, typename T::type U = 0> void b(T); void b(...);977 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}978 template<typename T, int U = T::value> void c(T); void c(...);979 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}980 template<typename T> void d(T, int = T::value); void d(...);981 // expected-error@-1 {{type 'int' cannot be used prior to '::' because it has no members}}982 // expected-note@#cwg575-d {{in instantiation of default function argument expression for 'd<int>' required here}}983 void x() {984 a(0);985 b(0);986 c(0);987 d(0); // #cwg575-d988 }989 990 template<typename T = int&> void f(T* = 0);991 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}992 template<typename T = int> void f(T = 0);993 // cxx98-error@-1 {{default template arguments for a function template are a C++11 extension}}994 void g() { f<>(); }995 996 template<typename T> T &h(T *);997 template<typename T> T *h(T *);998 void *p = h((void*)0);999} // namespace cwg5751000 1001namespace cwg576 { // cwg576: 3.51002 typedef void f() {}1003 // expected-error@-1 {{function definition declared 'typedef'}}1004 void f(typedef int n);1005 // expected-error@-1 {{invalid storage class specifier in function declarator}}1006 void f(char c) { typedef int n; }1007} // namespace cwg5761008 1009namespace cwg577 { // cwg577: 3.51010 typedef void V;1011 typedef const void CV;1012 void a(void);1013 void b(const void);1014 // expected-error@-1 {{'void' as parameter must not have type qualifiers}}1015 void c(V);1016 void d(CV);1017 // expected-error@-1 {{'void' as parameter must not have type qualifiers}}1018 void (*e)(void) = c;1019 void (*f)(const void);1020 // expected-error@-1 {{'void' as parameter must not have type qualifiers}}1021 void (*g)(V) = a;1022 void (*h)(CV);1023 // expected-error@-1 {{'void' as parameter must not have type qualifiers}}1024 template<typename T> void i(T); // #cwg577-i1025 template<typename T> void j(void (*)(T)); // #cwg577-j1026 void k() {1027 a();1028 c();1029 i<void>();1030 // expected-error@-1 {{no matching function for call to 'i'}}1031 // expected-note@#cwg577-i {{candidate function template not viable: requires 1 argument, but 0 were provided}}1032 i<const void>();1033 // expected-error@-1 {{no matching function for call to 'i'}}1034 // expected-note@#cwg577-i {{candidate function template not viable: requires 1 argument, but 0 were provided}}1035 j<void>(0);1036 // expected-error@-1 {{no matching function for call to 'j'}}1037 // expected-note@#cwg577-j {{candidate template ignored: substitution failure [with T = void]: argument may not have 'void' type}}1038 j<const void>(0);1039 // expected-error@-1 {{no matching function for call to 'j'}}1040 // expected-note@#cwg577-j {{candidate template ignored: substitution failure [with T = const void]: argument may not have 'void' type}}1041 }1042} // namespace cwg5771043 1044namespace cwg580 { // cwg580: partial1045 class C;1046 struct A { static C c; };1047 struct B { static C c; };1048 class C {1049 C(); // #cwg580-C-ctor1050 ~C(); // #cwg580-C-dtor1051 1052 typedef int I; // #cwg580-I1053 template<int> struct X;1054 template<int> friend struct Y;1055 template<int> void f();1056 template<int> friend void g();1057 friend struct A;1058 };1059 1060 template<C::I> struct C::X {};1061 template<C::I> struct Y {};1062 template<C::I> struct Z {};1063 // expected-error@-1 {{'I' is a private member of 'cwg580::C'}}1064 // expected-note@#cwg580-I {{implicitly declared private here}}1065 1066 struct C2 {1067 class X {1068 struct A;1069 typedef int I;1070 friend struct A;1071 };1072 class Y {1073 // FIXME: We incorrectly accept this1074 // because we think C2::Y::A<...> might1075 // instantiate to C2::X::A1076 template<X::I> struct A {};1077 };1078 };1079 1080 template<C::I> void C::f() {}1081 template<C::I> void g() {}1082 template<C::I> void h() {}1083 // expected-error@-1 {{'I' is a private member of 'cwg580::C'}}1084 // expected-note@#cwg580-I {{implicitly declared private here}}1085 1086 C A::c;1087 C B::c; // #cwg580-c1088 // expected-error@#cwg580-c {{calling a private constructor of class 'cwg580::C'}}1089 // expected-note@#cwg580-C-ctor {{implicitly declared private here}}1090 // expected-error@#cwg580-c {{variable of type 'C' has private destructor}}1091 // expected-note@#cwg580-C-dtor {{implicitly declared private here}}1092} // namespace cwg5801093 1094// cwg582: na1095 1096namespace cwg583 { // cwg583: 41097 // see n36241098 int *p;1099 bool b1 = p < 0;1100 // expected-error@-1 {{ordered comparison between pointer and zero ('int *' and 'int')}}1101 bool b2 = p > 0;1102 // expected-error@-1 {{ordered comparison between pointer and zero ('int *' and 'int')}}1103 bool b3 = p <= 0;1104 // expected-error@-1 {{ordered comparison between pointer and zero ('int *' and 'int')}}1105 bool b4 = p >= 0;1106 // expected-error@-1 {{ordered comparison between pointer and zero ('int *' and 'int')}}1107} // namespace cwg5831108 1109// cwg584: na1110 1111namespace cwg585 { // cwg585: 3.01112 template<typename> struct T; // #cwg585-struct-T1113 struct A {1114 friend T;1115 // cxx98-14-error@-1 {{a type specifier is required for all declarations}}1116 // cxx98-14-error@-2 {{friends can only be classes or functions}}1117 // since-cxx17-error@-3 {{use of class template 'T' requires template arguments; argument deduction not allowed in friend declaration}}1118 // since-cxx17-note@#cwg585-struct-T {{template is declared here}}1119 // FIXME: It's not clear whether the standard allows this or what it means,1120 // but the CWG585 writeup suggests it as an alternative.1121 template<typename U> friend T<U>;1122 // expected-error@-1 {{friend type templates must use an elaborated type}}1123 };1124 template<template<typename> class T> struct B { // #cwg585-template-T1125 friend T;1126 // cxx98-14-error@-1 {{a type specifier is required for all declarations}}1127 // cxx98-14-error@-2 {{friends can only be classes or functions}}1128 // since-cxx17-error@-3 {{use of template template parameter 'T' requires template arguments; argument deduction not allowed in friend declaration}}1129 // since-cxx17-note@#cwg585-template-T {{template is declared here}}1130 template<typename U> friend T<U>;1131 // expected-error@-1 {{friend type templates must use an elaborated type}}1132 };1133} // namespace cwg5851134 1135// cwg586: na1136 1137namespace cwg587 { // cwg587: 3.21138 template<typename T> void f(bool b, const T x, T y) {1139 const T *p = &(b ? x : y);1140 }1141 struct S {};1142 template void f(bool, const int, int);1143 template void f(bool, const S, S);1144} // namespace cwg5871145 1146namespace cwg588 { // cwg588: 2.71147 struct A { int n; }; // #cwg588-A1148 template<typename T> int f() {1149 struct S : A, T { int f() { return n; } } s;1150 int a = s.f();1151 int b = s.n;1152 // expected-error@-1 {{member 'n' found in multiple base classes of different types}}1153 // expected-note@#cwg588-k {{in instantiation of function template specialization 'cwg588::f<cwg588::B>' requested here}}1154 // expected-note@#cwg588-A {{member found by ambiguous name lookup}}1155 // expected-note@#cwg588-B {{member found by ambiguous name lookup}}1156 }1157 struct B { int n; }; // #cwg588-B1158 int k = f<B>(); // #cwg588-k1159} // namespace cwg5881160 1161namespace cwg589 { // cwg589: 2.71162 struct B { };1163 struct D : B { };1164 D f();1165 extern const B &b;1166 bool a;1167 const B *p = &(a ? f() : b);1168 // expected-error@-1 {{taking the address of a temporary object of type 'const B'}}1169 const B *q = &(a ? D() : b);1170 // expected-error@-1 {{taking the address of a temporary object of type 'const B'}}1171} // namespace cwg5891172 1173namespace cwg590 { // cwg590: 2.71174 template<typename T> struct A {1175 struct B {1176 struct C {1177 A<T>::B::C f(A<T>::B::C); // ok, no 'typename' required.1178 };1179 };1180 };1181 template<typename T> typename A<T>::B::C A<T>::B::C::f(A<T>::B::C) {}1182} // namespace cwg5901183 1184namespace cwg591 { // cwg591: 201185 template<typename T> struct A {1186 typedef int M;1187 struct B {1188 typedef void M;1189 struct C;1190 struct D;1191 };1192 };1193 1194 template<typename T> struct G {1195 struct B {1196 typedef int M;1197 struct C {1198 typedef void M;1199 struct D;1200 };1201 };1202 };1203 1204 template<typename T> struct H {1205 template<typename U> struct B {1206 typedef int M;1207 template<typename F> struct C {1208 typedef void M;1209 struct D;1210 struct P;1211 };1212 };1213 };1214 1215 template <typename, bool> struct M {1216 class P;1217 int M;1218 };1219 1220 template<typename T> struct A<T>::B::C : A<T> {1221 M m;1222 };1223 1224 template<typename T> struct G<T>::B::C::D : B {1225 M m;1226 };1227 1228 template<typename T>1229 template<typename U>1230 template<typename F>1231 struct H<T>::B<U>::C<F>::D : B<U> {1232 M m;1233 };1234 1235 template<typename T, bool B> class M<T,B>::P : M {1236 int foo() { (void) M; }1237 };1238 1239 template<typename T> struct A<T>::B::D : A<T*> {1240 M m;1241 // expected-error@-1 {{field has incomplete type 'M' (aka 'void'}}1242 };1243 1244 template<typename T>1245 template<typename U>1246 template<typename F>1247 struct H<T>::B<U>::C<F>::P : B<F> {1248 M m;1249 // expected-error@-1 {{field has incomplete type 'M' (aka 'void'}}1250 };1251} // namespace cwg5911252 1253// cwg592: na1254// cwg593 is in cwg593.cpp1255// cwg594: na1256 1257namespace cwg595 { // cwg595: dup 13301258 template<class T> struct X {1259 void f() throw(T) {}1260 // since-cxx17-error@-1 {{ISO C++17 does not allow dynamic exception specifications}}1261 // since-cxx17-note@-2 {{use 'noexcept(false)' instead}}1262 };1263 struct S {1264 X<S> xs;1265 };1266} // namespace cwg5951267 1268// cwg597: na1269 1270namespace cwg598 { // cwg598: 2.71271 namespace N {1272 void f(int);1273 void f(char);1274 // Not found by ADL.1275 void g(void (*)(int));1276 void h(void (*)(int));1277 1278 namespace M {1279 struct S {};1280 int &h(void (*)(S));1281 }1282 void i(M::S);1283 void i();1284 }1285 int &g(void(*)(char));1286 int &r = g(N::f);1287 int &s = h(N::f);1288 // expected-error@-1 {{use of undeclared identifier 'h'}}1289 int &t = h(N::i);1290} // namespace cwg5981291 1292namespace cwg599 { // cwg599: partial1293 typedef int Fn();1294 struct S { operator void*(); };1295 struct T { operator Fn*(); };1296 struct U { operator int*(); operator void*(); }; // #cwg599-U1297 struct V { operator int*(); operator Fn*(); };1298 void f(void *p, void (*q)(), S s, T t, U u, V v) {1299 delete p;1300 // cxx98-23-error@-1 {{cannot delete expression with pointer-to-'void' type 'void *'}}1301 // since-cxx26-error@-2 {{cannot delete pointer to incomplete type 'void'}}1302 delete q;1303 // expected-error@-1 {{cannot delete expression of type 'void (*)()'}}1304 delete s;1305 // cxx98-23-error@-1 {{cannot delete expression with pointer-to-'void' type 'void *'}}1306 // since-cxx26-error@-2 {{cannot delete pointer to incomplete type 'void'}}1307 delete t;1308 // expected-error@-1 {{cannot delete expression of type 'T'}}1309 // FIXME: This is valid, but is rejected due to a non-conforming GNU1310 // extension allowing deletion of pointers to void.1311 delete u;1312 // expected-error@-1 {{ambiguous conversion of delete expression of type 'U' to a pointer}}1313 // expected-note@#cwg599-U {{conversion to pointer type 'int *'}}1314 // expected-note@#cwg599-U {{conversion to pointer type 'void *'}}1315 delete v;1316 }1317} // namespace cwg5991318