p5-0x.cpp revision e653ba2f3b6d993b5d410554c12416c03ec7775b
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 2 3struct DefaultedDefCtor1 {}; 4struct DefaultedDefCtor2 { DefaultedDefCtor2() = default; }; 5struct DeletedDefCtor { DeletedDefCtor() = delete; DeletedDefCtor(int); }; 6class PrivateDefCtor { PrivateDefCtor() = default; public: PrivateDefCtor(int); }; 7struct DeletedDtor { ~DeletedDtor() = delete; }; 8class PrivateDtor { ~PrivateDtor() = default; }; 9class Friend { 10 Friend() = default; ~Friend() = default; 11 friend struct NotDeleted6c; 12 friend struct NotDeleted7i; 13 friend struct NotDeleted7j; 14 friend struct NotDeleted7k; 15}; 16struct UserProvidedDefCtor { UserProvidedDefCtor() {} }; 17int n; 18 19 20// A defaulted default constructor for a class X is defined as deleted if: 21 22// - X is a union-like class that has a variant member with a non-trivial 23// default constructor, 24union Deleted1a { UserProvidedDefCtor u; }; // expected-note {{defined here}} 25Deleted1a d1a; // expected-error {{implicitly-deleted default constructor}} 26union NotDeleted1a { DefaultedDefCtor1 nu; }; 27NotDeleted1a nd1a; 28union NotDeleted1b { DefaultedDefCtor2 nu; }; 29NotDeleted1b nd1b; 30 31// - any non-static data member with no brace-or-equal-initializer is of 32// reference type, 33class Deleted2a { // expected-note {{defined here}} 34 Deleted2a() = default; // expected-note {{declared here}} 35 int &a; 36}; 37Deleted2a d2a; // expected-error {{implicitly-deleted default constructor}} 38class NotDeleted2a { int &a = n; }; 39NotDeleted2a nd2a; 40class NotDeleted2b { int &a = error; }; // expected-error {{undeclared identifier}} 41NotDeleted2b nd2b; 42 43// - any non-variant non-static data member of const qualified type (or array 44// thereof) with no brace-or-equal-initializer does not have a user-provided 45// default constructor, 46class Deleted3a { const int a; }; // expected-note {{here}} \ 47 expected-warning {{does not declare any constructor}} \ 48 expected-note {{will never be initialized}} 49Deleted3a d3a; // expected-error {{implicitly-deleted default constructor}} 50class Deleted3b { const DefaultedDefCtor1 a[42]; }; // expected-note {{here}} 51Deleted3b d3b; // expected-error {{implicitly-deleted default constructor}} 52class Deleted3c { const DefaultedDefCtor2 a; }; // expected-note {{defined here}} 53Deleted3c d3c; // expected-error {{implicitly-deleted default constructor}} 54class NotDeleted3a { const int a = 0; }; 55NotDeleted3a nd3a; 56class NotDeleted3b { const DefaultedDefCtor1 a[42] = {}; }; 57NotDeleted3b nd3b; 58class NotDeleted3c { const DefaultedDefCtor2 a = DefaultedDefCtor2(); }; 59NotDeleted3c nd3c; 60union NotDeleted3d { const int a; int b; }; 61NotDeleted3d nd3d; 62// FIXME: this class should not have a deleted default constructor. 63union NotDeleted3e { const DefaultedDefCtor1 a[42]; int b; }; // unexpected-note {{here}} 64NotDeleted3e nd3e; // unexpected-error {{implicitly-deleted default constructor}} 65// FIXME: clang implements the pre-FDIS rule, under which DefaultedDefCtor2 is 66// non-trivial. 67union NotDeleted3f { const DefaultedDefCtor2 a; int b; }; // unexpected-note {{here}} 68NotDeleted3f nd3f; // unexpected-error {{implicitly-deleted default constructor}} 69 70// - X is a union and all of its variant members are of const-qualified type (or 71// array thereof), 72union Deleted4a { const int a; const int b; const UserProvidedDefCtor c; }; // expected-note {{here}} 73Deleted4a d4a; // expected-error {{implicitly-deleted default constructor}} 74union Deleted4b { const int a; int b; }; 75Deleted4b d4b; 76 77// - X is a non-union class and all members of any anonymous union member are of 78// const-qualified type (or array thereof), 79struct Deleted5a { union { const int a; }; union { int b; }; }; // expected-note {{here}} 80Deleted5a d5a; // expected-error {{implicitly-deleted default constructor}} 81struct Deleted5b { union { const int a; int b; }; union { const int c; int d; }; }; 82Deleted5b d5b; 83 84// - any direct or virtual base class, or non-static data member with no 85// brace-or-equal-initializer, has class type M (or array thereof) and either 86// M has no default constructor or overload resolution as applied to M's default 87// constructor results in an ambiguity or in a function that is deleted or 88// inaccessible from the defaulted default constructor, or 89struct Deleted6a : Deleted2a {}; // expected-note {{here}} 90Deleted6a d6a; // expected-error {{implicitly-deleted default constructor}} 91struct Deleted6b : virtual Deleted2a {}; // expected-note {{here}} 92Deleted6b d6b; // expected-error {{implicitly-deleted default constructor}} 93struct Deleted6c { Deleted2a a; }; // expected-note {{here}} 94Deleted6c d6c; // expected-error {{implicitly-deleted default constructor}} 95struct Deleted6d { DeletedDefCtor a; }; // expected-note {{here}} 96Deleted6d d6d; // expected-error {{implicitly-deleted default constructor}} 97struct NotDeleted6a { DeletedDefCtor a = 0; }; 98NotDeleted6a nd6a; 99struct Deleted6e { PrivateDefCtor a; }; // expected-note {{here}} 100Deleted6e d6e; // expected-error {{implicitly-deleted default constructor}} 101struct NotDeleted6b { PrivateDefCtor a = 0; }; 102NotDeleted6b nd6b; 103struct NotDeleted6c { Friend a; }; 104NotDeleted6c nd6c; 105 106// - any direct or virtual base class or non-static data member has a type with 107// a destructor that is deleted or inaccessible from the defaulted default 108// constructor. 109struct Deleted7a : DeletedDtor {}; // expected-note {{here}} 110Deleted7a d7a; // expected-error {{implicitly-deleted default constructor}} 111struct Deleted7b : virtual DeletedDtor {}; // expected-note {{here}} 112Deleted7b d7b; // expected-error {{implicitly-deleted default constructor}} 113struct Deleted7c { DeletedDtor a; }; // expected-note {{here}} 114Deleted7c d7c; // expected-error {{implicitly-deleted default constructor}} 115struct Deleted7d { DeletedDtor a = {}; }; // expected-note {{here}} 116Deleted7d d7d; // expected-error {{implicitly-deleted default constructor}} 117struct Deleted7e : PrivateDtor {}; // expected-note {{here}} 118Deleted7e d7e; // expected-error {{implicitly-deleted default constructor}} 119struct Deleted7f : virtual PrivateDtor {}; // expected-note {{here}} 120Deleted7f d7f; // expected-error {{implicitly-deleted default constructor}} 121struct Deleted7g { PrivateDtor a; }; // expected-note {{here}} 122Deleted7g d7g; // expected-error {{implicitly-deleted default constructor}} 123struct Deleted7h { PrivateDtor a = {}; }; // expected-note {{here}} 124Deleted7h d7h; // expected-error {{implicitly-deleted default constructor}} 125struct NotDeleted7i : Friend {}; 126NotDeleted7i d7i; 127struct NotDeleted7j : virtual Friend {}; 128NotDeleted7j d7j; 129struct NotDeleted7k { Friend a; }; 130NotDeleted7k d7k; 131 132 133class Trivial { static const int n = 42; }; 134static_assert(__has_trivial_constructor(Trivial), "Trivial is nontrivial"); 135 136// A default constructor is trivial if it is not user-provided and if: 137class NonTrivialDefCtor1 { NonTrivialDefCtor1(); }; 138static_assert(!__has_trivial_constructor(NonTrivialDefCtor1), "NonTrivialDefCtor1 is trivial"); 139 140// - its class has no virtual functions (10.3) and no virtual base classes (10.1), and 141class NonTrivialDefCtor2 { virtual void f(); }; 142static_assert(!__has_trivial_constructor(NonTrivialDefCtor2), "NonTrivialDefCtor2 is trivial"); 143class NonTrivialDefCtor3 : virtual Trivial {}; 144static_assert(!__has_trivial_constructor(NonTrivialDefCtor3), "NonTrivialDefCtor3 is trivial"); 145 146// - no non-static data member of its class has a brace-or-equal-initializer, and 147class NonTrivialDefCtor4 { int m = 52; }; 148static_assert(!__has_trivial_constructor(NonTrivialDefCtor4), "NonTrivialDefCtor4 is trivial"); 149 150// - all the direct base classes of its class have trivial default constructors, and 151class NonTrivialDefCtor5 : NonTrivialDefCtor1 {}; 152static_assert(!__has_trivial_constructor(NonTrivialDefCtor5), "NonTrivialDefCtor5 is trivial"); 153 154// - for all the non-static data members of its class that are of class type (or array thereof), each such class 155// has a trivial default constructor. 156class NonTrivialDefCtor6 { NonTrivialDefCtor1 t; }; 157static_assert(!__has_trivial_constructor(NonTrivialDefCtor6), "NonTrivialDefCtor5 is trivial"); 158 159// Otherwise, the default constructor is non-trivial. 160class Trivial2 { Trivial2() = delete; }; 161static_assert(__has_trivial_constructor(Trivial2), "Trivial2 is trivial"); 162 163class Trivial3 { Trivial3() = default; }; 164static_assert(__has_trivial_constructor(Trivial3), "Trivial3 is trivial"); 165 166template<typename T> class Trivial4 { Trivial4() = default; }; 167static_assert(__has_trivial_constructor(Trivial4<int>), "Trivial4 is trivial"); 168 169template<typename T> class Trivial5 { Trivial5() = delete; }; 170static_assert(__has_trivial_constructor(Trivial5<int>), "Trivial5 is trivial"); 171