726 lines · cpp
1// RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected,cxx98-14,cxx98 -fexceptions -fcxx-exceptions -pedantic-errors2// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,cxx11-20,cxx98-14,cxx11-17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors3// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,cxx11-20,since-cxx14,cxx98-14,cxx11-17,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors4// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,cxx11-20,since-cxx14,since-cxx17,cxx11-17,since-cxx11,since-cxx14,cxx17 -fexceptions -fcxx-exceptions -pedantic-errors5// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,cxx11-20,since-cxx14,since-cxx17,since-cxx20,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors6// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,since-cxx20,since-cxx23,since-cxx11,since-cxx14 -fexceptions -fcxx-exceptions -pedantic-errors7// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx14,since-cxx17,since-cxx20,since-cxx23,since-cxx11,since-cxx14 -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 14namespace cwg1800 { // cwg1800: 2.915struct A { union { int n; }; };16static_assert(__is_same(__decltype(&A::n), int A::*), "");17} // namespace cwg180018 19namespace cwg1801 { // cwg1801: 2.820static union {21 int i;22};23 24template <int &> struct S {}; // #cwg1801-S25S<i> V; // #cwg1801-S-i26// cxx98-14-error@-1 {{non-type template argument does not refer to any declaration}}27// cxx98-14-note@#cwg1801-S {{template parameter is declared here}}28// cxx17-error@#cwg1801-S-i {{non-type template argument refers to subobject '.i'}}29} // namespace cwg180130 31namespace cwg1802 { // cwg1802: 3.132#if __cplusplus >= 201103L33// Using a Wikipedia example of surrogate pair:34// https://en.wikipedia.org/wiki/UTF-16#Examples35constexpr char16_t a[3] = u"\U00010437";36static_assert(a[0] == 0xD801, "");37static_assert(a[1] == 0xDC37, "");38static_assert(a[2] == 0x0, "");39#endif40} // namespace cwg180241 42namespace cwg1803 { // cwg1803: 2.943#if __cplusplus >= 201103L44struct A {45 enum E : int;46 enum E : int {};47 enum class EC;48 enum class EC {};49 enum struct ES;50 enum struct ES {};51};52#endif53} // namespace cwg180354 55namespace cwg1804 { // cwg1804: 2.756template <typename, typename>57struct A {58 void f1();59 60 template <typename V>61 void f2(V);62 63 class B {64 void f3();65 };66 67 template <typename>68 class C {69 void f4();70 };71};72 73template <typename U>74struct A<int, U> {75 void f1();76 77 template <typename V>78 void f2(V);79 80 class B {81 void f3();82 };83 84 template <typename>85 class C {86 void f4();87 };88};89 90class D {91 int i;92 93 template <typename, typename>94 friend struct A;95};96 97template <typename U>98struct A<double, U> {99 void f1();100 101 template <typename V>102 void f2(V);103 104 class B {105 void f3();106 };107 108 template <typename>109 class C {110 void f4();111 };112};113 114template <typename U>115void A<int, U>::f1() {116 D d;117 d.i = 0;118}119 120template <typename U>121void A<double, U>::f1() {122 D d;123 d.i = 0;124}125 126template <typename U>127template <typename V>128void A<int, U>::f2(V) {129 D d;130 d.i = 0;131}132 133template <typename U>134template <typename V>135void A<double, U>::f2(V) {136 D d;137 d.i = 0;138}139 140template <typename U>141void A<int, U>::B::f3() {142 D d;143 d.i = 0;144}145 146template <typename U>147void A<double, U>::B::f3() {148 D d;149 d.i = 0;150}151 152template <typename U>153template <typename V>154void A<int, U>::C<V>::f4() {155 D d;156 d.i = 0;157}158 159template <typename U>160template <typename V>161void A<double, U>::C<V>::f4() {162 D d;163 d.i = 0;164}165} // namespace cwg1804166 167// cwg1807 is in cwg1807.cpp168 169namespace cwg1812 { // cwg1812: no170 // NB: dup 1710171#if __cplusplus >= 201103L172template <typename T> struct A {173 using B = typename T::C<int>;174 // since-cxx11-error@-1 {{use 'template' keyword to treat 'C' as a dependent template name}}175};176#endif177} // namespace cwg1812178 179namespace cwg1813 { // cwg1813: 7180 struct B { int i; };181 struct C : B {};182 struct D : C {};183 struct E : D { char : 4; };184 185 static_assert(__is_standard_layout(B), "");186 static_assert(__is_standard_layout(C), "");187 static_assert(__is_standard_layout(D), "");188 static_assert(!__is_standard_layout(E), "");189 190 struct Q {};191 struct S : Q {};192 struct T : Q {};193 struct U : S, T {};194 195 static_assert(__is_standard_layout(Q), "");196 static_assert(__is_standard_layout(S), "");197 static_assert(__is_standard_layout(T), "");198 static_assert(!__is_standard_layout(U), "");199}200 201namespace cwg1814 { // cwg1814: 3.1202#if __cplusplus >= 201103L203 void test() {204 auto lam = [](int x = 42) { return x; };205 }206#endif207} // namespace cwg1814208 209namespace cwg1815 { // cwg1815: 20210#if __cplusplus >= 201402L211 struct A { int &&r = 0; };212 A a = {};213 214 struct B { int &&r = 0; }; // #cwg1815-B215 // since-cxx14-error@-1 {{reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}216 // since-cxx14-note@#cwg1815-B {{initializing field 'r' with default member initializer}}217 // since-cxx14-note@#cwg1815-b {{in implicit default constructor for 'cwg1815::B' first required here}}218 B b; // #cwg1815-b219 220#if __cplusplus >= 201703L221 struct C { const int &r = 0; };222 constexpr C c = {}; // OK, since cwg1815223 static_assert(c.r == 0);224 225 constexpr int f() {226 A a = {}; // OK, since cwg1815227 return a.r;228 }229 static_assert(f() == 0);230#endif231#endif232} // namespace cwg1815233 234// cwg1818 is in cwg1818.cpp235 236namespace cwg1820 { // cwg1820: 3.5237typedef int A;238typedef int cwg1820::A;239// expected-warning@-1 {{extra qualification on member 'A'}}240// expected-error@-2 {{typedef declarator cannot be qualified}}241 242namespace B {243typedef int cwg1820::A;244// expected-error@-1 {{cannot define or redeclare 'A' here because namespace 'B' does not enclose namespace 'cwg1820'}}245// expected-error@-2 {{typedef declarator cannot be qualified}}246}247 248class C1 {249 typedef int cwg1820::A;250 // expected-error@-1 {{non-friend class member 'A' cannot have a qualified name}}251 // expected-error@-2 {{typedef declarator cannot be qualified}}252};253 254template <typename>255class C2 {256 typedef int cwg1820::A;257 // expected-error@-1 {{non-friend class member 'A' cannot have a qualified name}}258 // expected-error@-2 {{typedef declarator cannot be qualified}}259};260 261void d1() {262 typedef int cwg1820::A;263 // expected-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}264 // expected-error@-2 {{typedef declarator cannot be qualified}}265}266 267template<typename>268void d2() {269 typedef int cwg1820::A;270 // expected-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}271 // expected-error@-2 {{typedef declarator cannot be qualified}}272}273 274#if __cplusplus >= 201103L275auto e = [] {276 typedef int cwg1820::A;277 // since-cxx11-error@-1 {{definition or redeclaration of 'A' not allowed inside a function}}278 // since-cxx11-error@-2 {{typedef declarator cannot be qualified}}279};280#endif281} // namespace cwg1820282 283namespace cwg1821 { // cwg1821: 2.9284struct A {285 template <typename> struct B {286 void f();287 };288 template <typename T> void B<T>::f(){};289 // expected-error@-1 {{non-friend class member 'f' cannot have a qualified name}}290 291 struct C {292 void f();293 };294 void C::f() {}295 // expected-error@-1 {{non-friend class member 'f' cannot have a qualified name}}296};297} // namespace cwg1821298 299namespace cwg1822 { // cwg1822: 3.1300#if __cplusplus >= 201103L301 double a;302 auto x = [] (int a) {303 static_assert(__is_same(decltype(a), int), "should be resolved to lambda parameter");304 };305#endif306} // namespace cwg1822307 308namespace cwg1824 { // cwg1824: 2.7309template<typename T>310struct A {311 T t;312};313 314struct S {315 A<S> f() { return A<S>(); }316};317} // namespace cwg1824318 319namespace cwg1832 { // cwg1832: 3.0320enum E { // #cwg1832-E321 a = static_cast<int>(static_cast<E>(0))322 // expected-error@-1 {{'E' is an incomplete type}}323 // expected-note@#cwg1832-E {{definition of 'cwg1832::E' is not complete until the closing '}'}}324};325 326#if __cplusplus >= 201103L327enum E2: decltype(static_cast<E2>(0), 0) {};328// since-cxx11-error@-1 {{unknown type name 'E2'}}329enum class E3: decltype(static_cast<E3>(0), 0) {};330// since-cxx11-error@-1 {{unknown type name 'E3'}}331#endif332} // namespace cwg1832333 334namespace cwg1837 { // cwg1837: 3.3335#if __cplusplus >= 201103L336 template <typename T>337 struct Fish { static const bool value = true; };338 339 struct Other {340 int p();341 auto q() -> decltype(p()) *;342 };343 344 class Outer {345 friend auto Other::q() -> decltype(this->p()) *;346 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}347 int g();348 int f() {349 extern void f(decltype(this->g()) *);350 struct Inner {351 static_assert(Fish<decltype(this->g())>::value, "");352 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}353 enum { X = Fish<decltype(this->f())>::value };354 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}355 struct Inner2 : Fish<decltype(this->g())> { };356 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}357 friend void f(decltype(this->g()) *);358 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}359 friend auto Other::q() -> decltype(this->p()) *;360 // since-cxx11-error@-1 {{invalid use of 'this' outside of a non-static member function}}361 };362 return 0;363 }364 };365 366 struct A {367 int f();368 bool b = [] {369 // since-cxx11-warning@-1 {{address of lambda function pointer conversion operator will always evaluate to 'true'}}370 struct Local {371 static_assert(sizeof(this->f()) == sizeof(int), "");372 };373 };374 };375#endif376} // namespace cwg1837377 378namespace cwg1862 { // cwg1862: no379template<class T>380struct A {381 struct B {382 void e();383 };384 385 void f();386 387 struct D {388 void g();389 };390 391 T h();392 393 template<T U>394 T i();395};396 397template<>398struct A<int> {399 struct B {400 void e();401 };402 403 int f();404 405 struct D {406 void g();407 };408 409 template<int U>410 int i();411};412 413template<>414struct A<float*> {415 int* h();416};417 418class C {419 int private_int;420 421 template<class T>422 friend struct A<T>::B;423 // expected-warning@-1 {{dependent nested name specifier 'A<T>' for friend class declaration is not supported; turning off access control for 'C'}}424 425 template<class T>426 friend void A<T>::f();427 // expected-warning@-1 {{dependent nested name specifier 'A<T>' for friend class declaration is not supported; turning off access control for 'C'}}428 429 // FIXME: this is ill-formed, because A<T>::D does not end with a simple-template-id430 template<class T>431 friend void A<T>::D::g();432 // expected-warning@-1 {{dependent nested name specifier 'A<T>::D' for friend class declaration is not supported; turning off access control for 'C'}}433 434 template<class T>435 friend int *A<T*>::h();436 // expected-warning@-1 {{dependent nested name specifier 'A<T *>' for friend class declaration is not supported; turning off access control for 'C'}}437 438 template<class T>439 template<T U>440 friend T A<T>::i();441 // expected-warning@-1 {{dependent nested name specifier 'A<T>' for friend class declaration is not supported; turning off access control for 'C'}}442};443 444C c;445 446template<class T>447void A<T>::B::e() { (void)c.private_int; }448void A<int>::B::e() { (void)c.private_int; }449 450template<class T>451void A<T>::f() { (void)c.private_int; }452int A<int>::f() { (void)c.private_int; return 0; }453 454// FIXME: both definition of 'D::g' are not friends, so they don't have access to 'private_int'455template<class T>456void A<T>::D::g() { (void)c.private_int; }457void A<int>::D::g() { (void)c.private_int; }458 459template<class T>460T A<T>::h() { (void)c.private_int; }461int* A<float*>::h() { (void)c.private_int; return 0; }462 463template<class T>464template<T U>465T A<T>::i() { (void)c.private_int; }466template<int U>467int A<int>::i() { (void)c.private_int; }468} // namespace cwg1862469 470namespace cwg1872 { // cwg1872: 9471#if __cplusplus >= 201103L472 template<typename T> struct A : T {473 constexpr int f() const { return 0; }474 };475 struct X {};476 struct Y { virtual int f() const; };477 struct Z : virtual X {};478 479 constexpr int x = A<X>().f();480 constexpr int y = A<Y>().f();481 // cxx11-17-error@-1 {{constexpr variable 'y' must be initialized by a constant expression}}482 // cxx11-17-note@-2 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}}483#if __cplusplus >= 202002L484 static_assert(y == 0);485#endif486 // Note, this is invalid even though it would not use virtual dispatch.487 constexpr int y2 = A<Y>().A<Y>::f();488 // cxx11-17-error@-1 {{constexpr variable 'y2' must be initialized by a constant expression}}489 // cxx11-17-note@-2 {{cannot evaluate call to virtual function in a constant expression in C++ standards before C++20}}490#if __cplusplus >= 202002L491 static_assert(y2 == 0);492#endif493 constexpr int z = A<Z>().f();494 // since-cxx11-error@-1 {{constexpr variable 'z' must be initialized by a constant expression}}495 // cxx11-20-note@-2 {{non-literal type 'A<Z>' cannot be used in a constant expression}}496 // since-cxx23-note@-3 {{cannot construct object of type 'A<cwg1872::Z>' with virtual base class in a constant expression}}497#endif498} // namespace cwg1872499 500namespace cwg1878 { // cwg1878: 18501#if __cplusplus >= 201402L502#if __cplusplus >= 202002L503template <typename T>504concept C = true;505#endif506 507struct S {508 template <typename T>509 operator auto() const { return short(); }510 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}511 template <typename T>512 operator const auto() const { return int(); }513 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}514 template <typename T>515 operator const auto&() const { return char(); }516 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}517 template <typename T>518 operator const auto*() const { return long(); }519 // since-cxx14-error@-1 {{'auto' not allowed in declaration of conversion function template}}520 template <typename T>521 operator decltype(auto)() const { return unsigned(); }522 // since-cxx14-error@-1 {{'decltype(auto)' not allowed in declaration of conversion function template}}523#if __cplusplus >= 202002L524 template <typename T>525 operator C auto() const { return float(); }526 // since-cxx20-error@-1 {{'auto' not allowed in declaration of conversion function template}}527 template <typename T>528 operator C decltype(auto)() const { return double(); }529 // since-cxx20-error@-1 {{'decltype(auto)' not allowed in declaration of conversion function template}}530#endif531};532#endif533} // namespace cwg1878534 535namespace cwg1881 { // cwg1881: 7536 struct A { int a : 4; };537 struct B : A { int b : 3; };538 static_assert(__is_standard_layout(A), "");539 static_assert(!__is_standard_layout(B), "");540 541 struct C { int : 0; };542 struct D : C { int : 0; };543 static_assert(__is_standard_layout(C), "");544 static_assert(!__is_standard_layout(D), "");545} // namespace cwg1881546 547// cwg1884 is in cwg1884.cpp548 549namespace cwg1890 { // cwg1890: no drafting 2018-06-04550// FIXME: current consensus for CWG2335 is that the examples are well-formed.551namespace ex1 {552#if __cplusplus >= 201402L553struct A {554 struct B {555 auto foo() { return 0; } // #cwg1890-foo556 };557 decltype(B().foo()) x;558 // since-cxx14-error@-1 {{function 'foo' with deduced return type cannot be used before it is defined}}559 // since-cxx14-note@#cwg1890-foo {{'foo' declared here}}560};561#endif562} // namespace ex1563 564namespace ex2 {565#if __cplusplus >= 201103L566struct Bar {567 struct Baz { // #cwg1890-Baz568 int a = 0;569 };570 static_assert(__is_constructible(Baz), "");571 // since-cxx11-error@-1 {{static assertion failed due to requirement '__is_constructible(cwg1890::ex2::Bar::Baz)'}}572 // since-cxx11-note@#cwg1890-Baz {{'Baz' defined here}}573};574#endif575} // namespace ex2576} // namespace cwg1890577 578void cwg1891() { // cwg1891: 4579#if __cplusplus >= 201103L580 int n;581 auto a = []{}; // #cwg1891-a582 auto b = [=]{ return n; }; // #cwg1891-b583 typedef decltype(a) A;584 typedef decltype(b) B;585 586 static_assert(!__is_trivially_constructible(A), "");587 // since-cxx20-error@-1 {{failed}}588 static_assert(!__is_trivially_constructible(B), "");589 590 // C++20 allows default construction for non-capturing lambdas (P0624R2).591 A x;592 // cxx11-17-error@-1 {{no matching constructor for initialization of 'A' (aka '(lambda at}}593 // cxx11-17-note@#cwg1891-a {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}594 // cxx11-17-note@#cwg1891-a {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}595 B y;596 // since-cxx11-error@-1 {{no matching constructor for initialization of 'B' (aka '(lambda at}}597 // since-cxx11-note@#cwg1891-b {{candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided}}598 // since-cxx11-note@#cwg1891-b {{candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided}}599 600 // C++20 allows assignment for non-capturing lambdas (P0624R2).601 a = a;602 // cxx11-17-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}603 // cxx11-17-note@#cwg1891-a {{lambda expression begins here}}604 a = static_cast<A&&>(a);605 // cxx11-17-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}606 // cxx11-17-note@#cwg1891-a {{lambda expression begins here}}607 b = b;608 // since-cxx11-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}609 // since-cxx11-note@#cwg1891-b {{lambda expression begins here}}610 b = static_cast<B&&>(b);611 // since-cxx11-error-re@-1 {{{{object of type '\(lambda at .+\)' cannot be assigned because its copy assignment operator is implicitly deleted}}}}612 // since-cxx11-note@#cwg1891-b {{lambda expression begins here}}613#endif614} // void cwg1891()615 616namespace cwg1894 { // cwg1894: 3.8617 // NB: reusing part of cwg407 test618namespace A {619 struct S {};620}621namespace B {622 typedef int S;623}624namespace E {625 typedef A::S S;626 using A::S;627 struct S s;628}629namespace F {630 typedef A::S S;631}632namespace G {633 using namespace A;634 using namespace F;635 struct S s;636}637namespace H {638 using namespace F;639 using namespace A;640 struct S s;641}642} // namespace cwg1894643 644namespace cwg1898 { // cwg1898: 2.7645void e(int) {} // #cwg1898-e646void e(int) {}647// expected-error@-1 {{redefinition of 'e'}}648// expected-note@#cwg1898-e {{previous definition is here}}649 650void e2(int) {}651void e2(long) {} // OK, different type652 653void f(int) {} // #cwg1898-f654void f(const int) {}655// expected-error@-1 {{redefinition of 'f'}}656// expected-note@#cwg1898-f {{previous definition is here}}657 658void g(int) {} // #cwg1898-g659void g(volatile int) {}660// since-cxx20-warning@-1 {{volatile-qualified parameter type 'volatile int' is deprecated}}661// expected-error@-2 {{redefinition of 'g'}}662// expected-note@#cwg1898-g {{previous definition is here}}663 664void h(int *) {} // #cwg1898-h665void h(int[]) {}666// expected-error@-1 {{redefinition of 'h'}}667// expected-note@#cwg1898-h {{previous definition is here}}668 669void h2(int *) {} // #cwg1898-h2670void h2(int[2]) {}671// expected-error@-1 {{redefinition of 'h2'}}672// expected-note@#cwg1898-h2 {{previous definition is here}}673 674void h3(int (*)[2]) {} // #cwg1898-h3675void h3(int [3][2]) {}676// expected-error@-1 {{redefinition of 'h3'}}677// expected-note@#cwg1898-h3 {{previous definition is here}}678 679void h4(int (*)[2]) {}680void h4(int [3][3]) {} // OK, differ in non-top-level extent of array681 682void i(int *) {}683void i(const int *) {} // OK, pointee cv-qualification is not discarded684 685void i2(int *) {} // #cwg1898-i2686void i2(int * const) {}687// expected-error@-1 {{redefinition of 'i2'}}688// expected-note@#cwg1898-i2 {{previous definition is here}}689 690void j(void(*)()) {} // #cwg1898-j691void j(void()) {}692// expected-error@-1 {{redefinition of 'j'}}693// expected-note@#cwg1898-j {{previous definition is here}}694 695void j2(void(int)) {} // #cwg1898-j2696void j2(void(const int)) {}697// expected-error@-1 {{redefinition of 'j2'}}698// expected-note@#cwg1898-j2 {{previous definition is here}}699 700struct A {701 void k(int) {} // #cwg1898-k702 void k(int) {}703 // expected-error@-1 {{class member cannot be redeclared}}704 // expected-note@#cwg1898-k {{previous definition is here}}705};706 707struct B : A {708 void k(int) {} // OK, shadows A::k709};710 711void l() {}712void l(...) {}713 714#if __cplusplus >= 201103L715template <typename T>716void m(T) {}717template <typename... Ts>718void m(Ts...) {}719 720template <typename T, typename U>721void m2(T, U) {}722template <typename... Ts, typename U>723void m2(Ts..., U) {}724#endif725} // namespace cwg1898726