558 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,since-cxx11,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,since-cxx11 -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,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors5// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,cxx11-20,since-cxx14,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors6// RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx23,since-cxx14,since-cxx20,since-cxx17,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors7// RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx23,since-cxx14,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#if __cplusplus >= 201103L15namespace std {16 typedef decltype(sizeof(int)) size_t;17 18 template<typename E> class initializer_list {19 const E *begin;20 size_t size;21 22 public:23 initializer_list();24 };25} // std26#endif27 28namespace cwg1601 { // cwg1601: 1029enum E : char { e };30// cxx98-error@-1 {{enumeration types with a fixed underlying type are a C++11 extension}}31void f(char);32void f(int);33void g() {34 f(e);35}36} // namespace cwg160137 38namespace cwg1606 { // cwg1606: 3.139#if __cplusplus >= 201103L40 std::size_t test() {41 int i = 1;42 int j = 1;43 auto f = [=]{ return i + j; };44 return sizeof(f);45 }46#endif47} // namespace cwg160648 49namespace cwg1611 { // cwg1611: dup 165850 struct A { A(int); };51 struct B : virtual A { virtual void f() = 0; };52 struct C : B { C() : A(0) {} void f(); };53 C c;54} // namespace cwg161155 56namespace cwg1631 { // cwg1631: 3.757#if __cplusplus >= 201103L58 // Incorrect overload resolution for single-element initializer-list59 60 struct A { int a[1]; };61 struct B { B(int); };62 void f(B, int);63 void f(B, int, int = 0);64 void f(int, A);65 66 void test() {67 f({0}, {{1}});68 // since-cxx11-warning@-1 {{braces around scalar initializer}}69 }70 71 namespace with_error {72 void f(B, int); // TODO: expected- note {{candidate function}}73 void f(int, A); // #cwg1631-f74 void f(int, A, int = 0); // #cwg1631-f-int75 76 void test() {77 f({0}, {{1}});78 // since-cxx11-error@-1 {{call to 'f' is ambiguous}}79 // since-cxx11-note@#cwg1631-f {{candidate function}}80 // since-cxx11-note@#cwg1631-f-int {{candidate function}}81 }82 }83#endif84} // namespace cwg163185 86namespace cwg1638 { // cwg1638: 3.187#if __cplusplus >= 201103L88 template<typename T> struct A {89 enum class E; // #cwg1638-E90 enum class F : T; // #cwg1638-F91 };92 93 template<> enum class A<int>::E;94 template<> enum class A<int>::E {};95 template<> enum class A<int>::F : int;96 template<> enum class A<int>::F : int {};97 98 template<> enum class A<short>::E : int;99 template<> enum class A<short>::E : int {};100 101 template<> enum class A<short>::F;102 // since-cxx11-error@-1 {{enumeration redeclared with different underlying type 'int' (was 'short')}}103 // since-cxx11-note@#cwg1638-F {{previous declaration is here}}104 template<> enum class A<char>::E : char;105 // since-cxx11-error@-1 {{enumeration redeclared with different underlying type 'char' (was 'int')}}106 // since-cxx11-note@#cwg1638-E {{previous declaration is here}}107 template<> enum class A<char>::F : int;108 // since-cxx11-error@-1 {{enumeration redeclared with different underlying type 'int' (was 'char')}}109 // since-cxx11-note@#cwg1638-F {{previous declaration is here}}110 111 enum class A<unsigned>::E;112 // since-cxx11-error@-1 {{template specialization requires 'template<>'}}113 template enum class A<unsigned>::E;114 // since-cxx11-error@-1 {{enumerations cannot be explicitly instantiated}}115 enum class A<unsigned>::E *e;116 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}117 118 struct B {119 friend enum class A<unsigned>::E;120 // since-cxx11-error@-1 {{reference to enumeration must use 'enum' not 'enum class'}}121 // since-cxx11-error@-2 {{elaborated enum specifier cannot be declared as a friend}}122 // since-cxx11-note@-3 {{remove 'enum class' to befriend an enum}}123 };124#endif125} // namespace cwg1638126 127namespace cwg1645 { // cwg1645: 3.9128#if __cplusplus >= 201103L129 struct A {130 constexpr A(int, float = 0); // #cwg1645-int-float131 explicit A(int, int = 0); // #cwg1645-int-int132 A(int, int, int = 0) = delete; // #cwg1645-int-int-int133 };134 135 struct B : A {136 using A::A; // #cwg1645-using137 };138 139 constexpr B a(0);140 // since-cxx11-error@-1 {{call to constructor of 'const B' is ambiguous}}141 // since-cxx11-note@#cwg1645-int-float {{candidate inherited constructor}}142 // since-cxx11-note@#cwg1645-using {{constructor from base class 'A' inherited here}}143 // since-cxx11-note@#cwg1645-int-int {{candidate inherited constructor}}144 // since-cxx11-note@#cwg1645-using {{constructor from base class 'A' inherited here}}145 constexpr B b(0, 0);146 // since-cxx11-error@-1 {{call to constructor of 'const B' is ambiguous}}147 // since-cxx11-note@#cwg1645-int-int {{candidate inherited constructor}}148 // since-cxx11-note@#cwg1645-using {{constructor from base class 'A' inherited here}}149 // since-cxx11-note@#cwg1645-int-int-int {{candidate inherited constructor has been explicitly deleted}}150 // since-cxx11-note@#cwg1645-using {{constructor from base class 'A' inherited here}}151#endif152} // namespace cwg1645153 154namespace cwg1652 { // cwg1652: 3.6155 int a, b;156 static_assert(&a + 1 == &b, "");157 // expected-error@-1 {{static assertion expression is not an integral constant expression}}158 // expected-note@-2 {{comparison against pointer '&a + 1' that points past the end of a complete object has unspecified value}}159} // namespace cwg1652160 161namespace cwg1653 { // cwg1653: 4 c++17162 void f(bool b) {163 ++b;164 // cxx98-14-warning@-1 {{incrementing expression of type bool is deprecated and incompatible with C++17}}165 // since-cxx17-error@-2 {{SO C++17 does not allow incrementing expression of type bool}}166 b++;167 // cxx98-14-warning@-1 {{incrementing expression of type bool is deprecated and incompatible with C++17}}168 // since-cxx17-error@-2 {{SO C++17 does not allow incrementing expression of type bool}}169 --b;170 // expected-error@-1 {{cannot decrement expression of type bool}}171 b--;172 // expected-error@-1 {{cannot decrement expression of type bool}}173 b += 1; // ok174 b -= 1; // ok175 }176} // namespace cwg1653177 178namespace cwg1658 { // cwg1658: 5179 namespace DefCtor {180 class A { A(); }; // #cwg1658-A1181 class B { ~B(); }; // #cwg1658-B1182 183 // The stars align! An abstract class does not construct its virtual bases.184 struct C : virtual A { C(); virtual void foo() = 0; };185 C::C() = default; // ok, not deleted186 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}187 struct D : virtual B { D(); virtual void foo() = 0; };188 D::D() = default; // ok, not deleted189 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}190 191 // In all other cases, we are not so lucky.192 struct E : A { E(); virtual void foo() = 0; }; // #cwg1658-E1193 E::E() = default; // #cwg1658-E1-ctor194 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}195 // cxx98-error@-2 {{base class 'A' has private default constructor}}196 // cxx98-note@-3 {{in defaulted default constructor for 'cwg1658::DefCtor::E' first required here}}197 // cxx98-note@#cwg1658-A1 {{implicitly declared private here}}198 // since-cxx11-error@#cwg1658-E1-ctor {{defaulting this default constructor would delete it after its first declaration}}199 // since-cxx11-note@#cwg1658-E1 {{default constructor of 'E' is implicitly deleted because base class 'A' has an inaccessible default constructor}}200 struct F : virtual A { F(); }; // #cwg1658-F1201 F::F() = default; // #cwg1658-F1-ctor202 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}203 // cxx98-error@-2 {{inherited virtual base class 'A' has private default constructor}}204 // cxx98-note@-3 {{in defaulted default constructor for 'cwg1658::DefCtor::F' first required here}}205 // cxx98-note@#cwg1658-A1 {{implicitly declared private here}}206 // since-cxx11-error@#cwg1658-F1-ctor {{defaulting this default constructor would delete it after its first declaration}}207 // since-cxx11-note@#cwg1658-F1 {{default constructor of 'F' is implicitly deleted because base class 'A' has an inaccessible default constructor}}208 209 struct G : B { G(); virtual void foo() = 0; }; // #cwg1658-G1210 G::G() = default; // #cwg1658-G1-ctor211 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}212 // cxx98-error@#cwg1658-G1 {{base class 'B' has private destructor}}213 // cxx98-note@#cwg1658-G1-ctor {{in defaulted default constructor for 'cwg1658::DefCtor::G' first required here}}214 // cxx98-note@#cwg1658-B1 {{implicitly declared private here}}215 // since-cxx11-error@#cwg1658-G1-ctor {{defaulting this default constructor would delete it after its first declaration}}216 // since-cxx11-note@#cwg1658-G1 {{default constructor of 'G' is implicitly deleted because base class 'B' has an inaccessible destructor}}217 struct H : virtual B { H(); }; // #cwg1658-H1218 H::H() = default; // #cwg1658-H1-ctor219 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}220 // cxx98-error@#cwg1658-H1 {{base class 'B' has private destructor}}221 // cxx98-note@#cwg1658-H1-ctor {{in defaulted default constructor for 'cwg1658::DefCtor::H' first required here}}222 // cxx98-note@#cwg1658-B1 {{implicitly declared private here}}223 // since-cxx11-error@#cwg1658-H1-ctor {{defaulting this default constructor would delete it after its first declaration}}224 // since-cxx11-note@#cwg1658-H1 {{default constructor of 'H' is implicitly deleted because base class 'B' has an inaccessible destructor}}225 }226 227 namespace Dtor {228 class B { ~B(); }; // #cwg1658-B2229 230 struct D : virtual B { ~D(); virtual void foo() = 0; };231 D::~D() = default; // ok, not deleted232 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}233 234 struct G : B { ~G(); virtual void foo() = 0; }; // #cwg1658-G2235 G::~G() = default; // #cwg1658-G2-dtor236 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}237 // cxx98-error@#cwg1658-G2 {{base class 'B' has private destructor}}238 // cxx98-note@#cwg1658-G2-dtor {{in defaulted destructor for 'cwg1658::Dtor::G' first required here}}239 // cxx98-note@#cwg1658-B2 {{implicitly declared private here}}240 // since-cxx11-error@#cwg1658-G2-dtor {{defaulting this destructor would delete it after its first declaration}}241 // since-cxx11-note@#cwg1658-G2 {{destructor of 'G' is implicitly deleted because base class 'B' has an inaccessible destructor}}242 struct H : virtual B { ~H(); }; // #cwg1658-H2243 H::~H() = default; // #cwg1658-H2-dtor244 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}245 // cxx98-error@#cwg1658-H2 {{base class 'B' has private destructor}}246 // cxx98-note@#cwg1658-H2-dtor {{in defaulted destructor for 'cwg1658::Dtor::H' first required here}}247 // cxx98-note@#cwg1658-B2 {{implicitly declared private here}}248 // since-cxx11-error@#cwg1658-H2-dtor {{defaulting this destructor would delete it after its first declaration}}249 // since-cxx11-note@#cwg1658-H2 {{destructor of 'H' is implicitly deleted because base class 'B' has an inaccessible destructor}}250 }251 252 namespace MemInit {253 struct A { A(int); }; // #cwg1658-A3254 struct B : virtual A {255 B() {}256 virtual void f() = 0;257 };258 struct C : virtual A {259 C() {}260 // expected-error@-1 {{constructor for 'cwg1658::MemInit::C' must explicitly initialize the base class 'A' which does not have a default constructor}}261 // expected-note@#cwg1658-A3 {{'cwg1658::MemInit::A' declared here}}262 };263 }264 265 namespace CopyCtorParamType {266 struct A { A(A&); };267 struct B : virtual A { virtual void f() = 0; };268 struct C : virtual A { virtual void f(); };269 struct D : A { virtual void f() = 0; }; // since-cxx23-note {{previous declaration is here}}270 271 struct X {272 friend B::B(const B&) throw();273 friend C::C(C&);274 friend D::D(D&); // since-cxx23-error {{non-constexpr declaration of 'D' follows constexpr declaration}}275 };276 }277 278 namespace CopyCtor {279 class A { A(const A&); A(A&&); }; // #cwg1658-A5280 // cxx98-error@-1 {{rvalue references are a C++11 extension}}281 282 struct C : virtual A { C(const C&); C(C&&); virtual void foo() = 0; };283 // cxx98-error@-1 {{rvalue references are a C++11 extension}}284 C::C(const C&) = default;285 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}286 C::C(C&&) = default;287 // cxx98-error@-1 {{rvalue references are a C++11 extension}}288 // cxx98-error@-2 {{defaulted function definitions are a C++11 extension}}289 290 struct E : A { E(const E&); E(E&&); virtual void foo() = 0; }; // #cwg1658-E5291 // cxx98-error@-1 {{rvalue references are a C++11 extension}}292 E::E(const E&) = default; // #cwg1658-E5-copy-ctor293 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}294 // cxx98-error@-2 {{base class 'A' has private copy constructor}}295 // cxx98-note@-3 {{in defaulted copy constructor for 'cwg1658::CopyCtor::E' first required here}}296 // cxx98-note@#cwg1658-A5 {{implicitly declared private here}}297 // since-cxx11-error@#cwg1658-E5-copy-ctor {{defaulting this copy constructor would delete it after its first declaration}}298 // since-cxx11-note@#cwg1658-E5 {{copy constructor of 'E' is implicitly deleted because base class 'A' has an inaccessible copy constructor}}299 E::E(E&&) = default; // #cwg1658-E5-move-ctor300 // cxx98-error@-1 {{rvalue references are a C++11 extension}}301 // cxx98-error@-2 {{defaulted function definitions are a C++11 extension}}302 // cxx98-error@-3 {{base class 'A' has private move constructor}}303 // cxx98-note@-4 {{in defaulted move constructor for 'cwg1658::CopyCtor::E' first required here}}304 // cxx98-note@#cwg1658-A5 {{implicitly declared private here}}305 // since-cxx11-error@#cwg1658-E5-move-ctor {{defaulting this move constructor would delete it after its first declaration}}306 // since-cxx11-note@#cwg1658-E5 {{move constructor of 'E' is implicitly deleted because base class 'A' has an inaccessible move constructor}}307 struct F : virtual A { F(const F&); F(F&&); }; // #cwg1658-F5308 // cxx98-error@-1 {{rvalue references are a C++11 extension}}309 F::F(const F&) = default; // #cwg1658-F5-copy-ctor310 // cxx98-error@-1 {{defaulted function definitions are a C++11 extension}}311 // cxx98-error@-2 {{inherited virtual base class 'A' has private copy constructor}}312 // cxx98-note@-3 {{in defaulted copy constructor for 'cwg1658::CopyCtor::F' first required here}}313 // cxx98-note@#cwg1658-A5 {{implicitly declared private here}}314 // since-cxx11-error@#cwg1658-F5-copy-ctor {{defaulting this copy constructor would delete it after its first declaration}}315 // since-cxx11-note@#cwg1658-F5 {{copy constructor of 'F' is implicitly deleted because base class 'A' has an inaccessible copy constructor}}316 F::F(F&&) = default; // #cwg1658-F5-move-ctor317 // cxx98-error@-1 {{rvalue references are a C++11 extension}}318 // cxx98-error@-2 {{defaulted function definitions are a C++11 extension}}319 // cxx98-error@-3 {{inherited virtual base class 'A' has private move constructor}}320 // cxx98-note@-4 {{in defaulted move constructor for 'cwg1658::CopyCtor::F' first required here}}321 // cxx98-note@#cwg1658-A5 {{implicitly declared private here}}322 // since-cxx11-error@#cwg1658-F5-move-ctor {{defaulting this move constructor would delete it after its first declaration}}323 // since-cxx11-note@#cwg1658-F5 {{move constructor of 'F' is implicitly deleted because base class 'A' has an inaccessible move constructor}}324 }325 326 // assignment case is superseded by cwg2180327} // namespace cwg1658328 329namespace cwg1672 { // cwg1672: 7330 struct Empty {};331 struct A : Empty {};332 struct B { Empty e; };333 struct C : A { B b; int n; };334 struct D : A { int n; B b; };335 336 static_assert(!__is_standard_layout(C), "");337 static_assert(__is_standard_layout(D), "");338 339 struct E { B b; int n; };340 struct F { int n; B b; };341 union G { B b; int n; };342 union H { int n; B b; };343 344 struct X {};345 template<typename T> struct Y : X, A { T t; };346 347 static_assert(!__is_standard_layout(Y<E>), "");348 static_assert(__is_standard_layout(Y<F>), "");349 static_assert(!__is_standard_layout(Y<G>), "");350 static_assert(!__is_standard_layout(Y<H>), "");351 static_assert(!__is_standard_layout(Y<X>), "");352} // namespace cwg1672353 354namespace cwg1684 { // cwg1684: 3.6355#if __cplusplus >= 201103L356 struct NonLiteral { // #cwg1684-struct357 NonLiteral();358 constexpr int f() { return 0; }359 // cxx11-warning@-1 {{'constexpr' non-static member function will not be implicitly 'const' in C++14; add 'const' to avoid a change in behavior}}360 };361 constexpr int f(NonLiteral &) { return 0; }362 constexpr int f(NonLiteral) { return 0; }363 // cxx11-20-error@-1 {{constexpr function's 1st parameter type 'NonLiteral' is not a literal type}}364 // cxx11-20-note@#cwg1684-struct {{'NonLiteral' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors}}365#endif366} // namespace cwg1684367 368namespace cwg1687 { // cwg1687: 7369 template<typename T> struct To {370 operator T(); // #cwg1687-op-T371 };372 373 int *a = To<int*>() + 100.0;374 // expected-error@-1 {{invalid operands to binary expression ('To<int *>' and 'double')}}375 // expected-note@#cwg1687-op-T {{first operand was implicitly converted to type 'int *'}}376 // since-cxx20-note@#cwg1687-op-T {{second operand was implicitly converted to type 'cwg1687::E2'}}377 int *b = To<int*>() + To<double>();378 // expected-error@-1 {{invalid operands to binary expression ('To<int *>' and 'To<double>')}}379 // expected-note@#cwg1687-op-T {{first operand was implicitly converted to type 'int *'}}380 // expected-note@#cwg1687-op-T {{second operand was implicitly converted to type 'double'}}381 382#if __cplusplus >= 202002L383 enum E1 {};384 enum E2 {};385 auto c = To<E1>() <=> To<E2>();386 // since-cxx20-error@-1 {{invalid operands to binary expression ('To<E1>' and 'To<E2>')}}387 // since-cxx20-note@#cwg1687-op-T {{operand was implicitly converted to type 'cwg1687::E}}388#endif389} // namespace cwg1687390 391namespace cwg1690 { // cwg1690: 9392 // See also the various tests in "CXX/basic/basic.lookup/basic.lookup.argdep".393#if __cplusplus >= 201103L394 namespace N {395 static auto lambda = []() { struct S {} s; return s; };396 void f(decltype(lambda()));397 }398 399 void test() {400 auto s = N::lambda();401 f(s); // ok402 }403#endif404} // namespace cwg1690405 406namespace cwg1691 { // cwg1691: 9407#if __cplusplus >= 201103L408 namespace N {409 namespace M {410 enum E : int;411 void f(E);412 }413 enum M::E : int {};414 void g(M::E); // #cwg1691-g415 }416 void test() {417 N::M::E e;418 f(e); // ok419 g(e);420 // since-cxx11-error@-1 {{use of undeclared identifier 'g'; did you mean 'N::g'?}}421 // since-cxx11-note@#cwg1691-g {{'N::g' declared here}}422 }423#endif424} // namespace cwg1691425 426namespace cwg1692 { // cwg1692: 9427 namespace N {428 struct A {429 struct B {430 struct C {};431 };432 };433 void f(A::B::C);434 }435 void test() {436 N::A::B::C c;437 f(c); // ok438 }439} // namespace cwg1692440 441namespace cwg1696 { // cwg1696: 7442 namespace std_examples {443#if __cplusplus >= 201402L444 extern struct A a;445 struct A {446 const A &x = { A{a, a} };447 const A &y = { A{} };448 // since-cxx14-error@-1 {{default member initializer for 'y' needed within definition of enclosing class 'A' outside of member functions}}449 // since-cxx14-note@-2 {{default member initializer declared here}}450 };451 A a{a, a};452 453 struct A1 {454 A1() : v(42) {}455 // since-cxx14-error@-1 {{reference member 'v' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}456 // since-cxx14-note@#cwg1696-A1 {{reference member declared here}}457 const int &v; // #cwg1696-A1458 };459 460 struct A2 {461 A2() = default;462 // since-cxx14-error@-1 {{reference member 'v' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}463 // since-cxx14-note-re@#cwg1696-A2-b {{in defaulted default constructor for {{.*}} first required here}}464 // since-cxx14-note@#cwg1696-A2-a {{initializing field 'v' with default member initializer}}465 A2(int v) : v(v) {}466 // since-cxx14-warning@-1 {{binding reference member 'v' to stack allocated parameter 'v'}}467 // since-cxx14-note@#cwg1696-A2-a {{reference member declared here}}468 const int &v = 42; // #cwg1696-A2-a469 };470 A2 a1; // #cwg1696-A2-b471 472 A2 a2(1); // OK, unfortunately473#endif474 }475 476 struct A { A(); ~A(); };477#if __cplusplus >= 201103L478 struct B {479 A &&a; // #cwg1696-a480 B() : a{} {}481 // since-cxx11-error@-1 {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}482 // since-cxx11-note@#cwg1696-a {{reference member declared here}}483 } b;484#endif485 486 struct C {487 C();488 const A &a; // #cwg1696-C-a489 };490 C::C() : a(A()) {}491 // expected-error@-1 {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}492 // expected-note@#cwg1696-C-a {{reference member declared here}}493 494#if __cplusplus >= 201103L495 // This is OK in C++14 onwards, per CWG1815, though we don't support that yet:496 // D1 d1 = {};497 // is equivalent to498 // D1 d1 = {A()};499 // ... which lifetime-extends the A temporary.500 struct D1 {501 // cxx11-error@-1 {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}502 // cxx11-note@#cwg1696-d1 {{in implicit default constructor for 'cwg1696::D1' first required here}}503 // cxx11-note@#cwg1696-D1-a {{initializing field 'a' with default member initializer}}504 const A &a = A(); // #cwg1696-D1-a505 };506 D1 d1 = {}; // #cwg1696-d1507 508 struct D2 {509 const A &a = A(); // #cwg1696-D2-a510 D2() {}511 // since-cxx11-error@-1 {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}512 // since-cxx11-note@#cwg1696-D2-a {{initializing field 'a' with default member initializer}}513 };514 515 struct D3 {516 // since-cxx11-error@-1 {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}517 // since-cxx11-note@#cwg1696-d3 {{in implicit default constructor for 'cwg1696::D3' first required here}}518 // since-cxx11-note@#cwg1696-D3-a {{initializing field 'a' with default member initializer}}519 const A &a = A(); // #cwg1696-D3-a520 };521 D3 d3; // #cwg1696-d3522 523 struct haslist1 {524 std::initializer_list<int> il; // #cwg1696-il-1525 haslist1(int i) : il{i, 2, 3} {}526 // since-cxx11-error@-1 {{backing array for 'std::initializer_list' member 'il' is a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}527 // since-cxx11-note@#cwg1696-il-1 {{'std::initializer_list' member declared here}}528 };529 530 struct haslist2 {531 std::initializer_list<int> il; // #cwg1696-il-2532 haslist2();533 };534 haslist2::haslist2() : il{1, 2} {}535 // since-cxx11-error@-1 {{backing array for 'std::initializer_list' member 'il' is a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}536 // since-cxx11-note@#cwg1696-il-2 {{'std::initializer_list' member declared here}}537 538 struct haslist3 {539 std::initializer_list<int> il = {1, 2, 3};540 };541 542 struct haslist4 {543 // since-cxx11-error@-1 {{backing array for 'std::initializer_list' member 'il' is a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}544 // since-cxx11-note@#cwg1696-hl4 {{in implicit default constructor for 'cwg1696::haslist4' first required here}}545 // since-cxx11-note@#cwg1696-il-4 {{initializing field 'il' with default member initializer}}546 std::initializer_list<int> il = {1, 2, 3}; // #cwg1696-il-4547 };548 haslist4 hl4; // #cwg1696-hl4549 550 struct haslist5 {551 std::initializer_list<int> il = {1, 2, 3}; // #cwg1696-il-5552 haslist5() {}553 // since-cxx11-error@-1 {{backing array for 'std::initializer_list' member 'il' is a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}554 // since-cxx11-note@#cwg1696-il-5 {{nitializing field 'il' with default member initializer}}555 };556#endif557} // namespace cwg1696558