xref: /llvm-project/clang/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p1.cpp (revision c00e44656dfbcb400af8d7f4fabf8f04d188104d)
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
3 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
4 
5 // An aggregate is an array or a class...
6 struct Aggr {
7 private:
8   static const int n;
9   void f();
10 protected:
11   struct Inner { int m; };
12 public:
13   bool &br;
14 };
15 bool b;
16 Aggr ag = { b };
17 
18 // with no user-provided constructors, ...
19 struct NonAggr1a { // expected-note 2 {{candidate constructor}}
20   NonAggr1a(int, int); // expected-note {{candidate constructor}}
21   int k;
22 };
23 NonAggr1a na1a = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr1a'}}
24 
25 struct NonAggr1b {
26   NonAggr1b(const NonAggr1b &); // expected-note {{candidate constructor}}
27   int k;
28 };
29 NonAggr1b na1b = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr1b'}}
30 
31 // no brace-or-equal-initializers for non-static data members, ...
32 // Note, this bullet was removed in C++1y.
33 struct NonAggr2 {
34   int m = { 123 };
35 };
36 NonAggr2 na2 = { 42 };
37 #if __cplusplus < 201402L
38 // expected-error@-2 {{no matching constructor for initialization of 'NonAggr2'}}
39 // expected-note@-6 3 {{candidate constructor}}
40 #endif
41 
42 // no private...
43 struct NonAggr3 { // expected-note 3 {{candidate constructor}}
44 private:
45   int n;
46 };
47 NonAggr3 na3 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr3'}}
48 
49 // or protected non-static data members, ...
50 struct NonAggr4 { // expected-note 3 {{candidate constructor}}
51 protected:
52   int n;
53 };
54 NonAggr4 na4 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr4'}}
55 
56 // [pre-C++1z] no base classes, ...
57 struct NonAggr5 : Aggr {
58 };
59 NonAggr5 na5 = { b };
60 #if __cplusplus <= 201402L
61 // expected-error@-2 {{no matching constructor for initialization of 'NonAggr5'}}
62 // expected-note@-5 3 {{candidate constructor}}
63 #endif
64 template<typename...BaseList>
65 struct MaybeAggr5a : BaseList... {};
66 MaybeAggr5a<> ma5a0 = {}; // ok
67 MaybeAggr5a<Aggr> ma5a1 = {}; // ok in C++17
68 MaybeAggr5a<NonAggr2> m5a2 = {}; // ok, aggregate init in C++17, default ctor in C++11 and C++14
69 MaybeAggr5a<NonAggr2> m5a3 = {0}; // ok in C++17, overrides default member initializer in base class
70 #if __cplusplus <= 201402L
71 // expected-error@-4 {{call to implicitly-deleted default constructor of 'MaybeAggr5a<Aggr>'}}
72 // expected-note@-7 {{default constructor of 'MaybeAggr5a<Aggr>' is implicitly deleted because base class 'Aggr' has a deleted default constructor}}
73 // expected-note@13 {{default constructor of 'Aggr' is implicitly deleted because field 'br' of reference type 'bool &' would not be initialized}}
74 // expected-error@-5 {{no matching constructor}} expected-note@-9 3{{candidate}}
75 #else
76 // expected-error@-9 {{reference member of type 'bool &' uninitialized}}
77 // expected-note@13 {{uninitialized reference member is here}}
78 #endif
79 
80 // [C++1z] no virtual, protected, or private base classes, ...
81 struct NonAggr5b : virtual Aggr {}; // expected-note 3{{candidate}}
82 NonAggr5b na5b = { b }; // expected-error {{no matching constructor}}
83 struct NonAggr5c : NonAggr5b {}; // expected-note 3{{candidate}}
84 NonAggr5c na5c = { b }; // expected-error {{no matching constructor}}
85 struct NonAggr5d : protected Aggr {}; // expected-note 3{{candidate}}
86 NonAggr5d na5d = { b }; // expected-error {{no matching constructor}}
87 struct NonAggr5e : private Aggr {}; // expected-note 3{{candidate}}
88 NonAggr5e na5e = { b }; // expected-error {{no matching constructor}}
89 class NonAggr5f : Aggr {}; // expected-note 3{{candidate}}
90 NonAggr5f na5f = { b }; // expected-error {{no matching constructor}}
91 
92 // [C++1z] (the base class need not itself be an aggregate)
93 struct MaybeAggr5g : NonAggr1a {};
94 MaybeAggr5g ma5g1 = { 1 };
95 MaybeAggr5g ma5g2 = { {1, 2} };
96 MaybeAggr5g ma5g3 = {};
97 #if __cplusplus <= 201402L
98 // expected-error@-4 {{no matching constructor}} // expected-note@-5 3{{candidate}}
99 // expected-error@-4 {{no matching constructor}} // expected-note@-6 3{{candidate}}
100 // expected-error@-4 {{implicitly-deleted default constructor}} expected-note@-7 {{no default constructor}}
101 #else
102 // expected-error@-8 {{no viable conversion from 'int' to 'NonAggr1a'}} expected-note@19 2{{candidate}}
103 // (ok)
104 // expected-error@-8 {{no matching constructor}} expected-note@19 2{{candidate}} expected-note@20 {{candidate}}
105 #endif
106 
107 // and no virtual functions.
108 struct NonAggr6 { // expected-note 3 {{candidate constructor}}
109   virtual void f();
110   int n;
111 };
112 NonAggr6 na6 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr6'}}
113 
114 struct NonAggr7 : NonAggr6 { // expected-note 3 {{candidate constructor}}
115   int n;
116 };
117 NonAggr7 na7 = {{}, 42}; // expected-error {{no matching constructor for initialization of 'NonAggr7'}}
118 
119 struct DefaultedAggr {
120   int n;
121 
122   DefaultedAggr() = default;
123   DefaultedAggr(const DefaultedAggr &) = default;
124   DefaultedAggr(DefaultedAggr &&) = default;
125   DefaultedAggr &operator=(const DefaultedAggr &) = default;
126   DefaultedAggr &operator=(DefaultedAggr &&) = default;
127   ~DefaultedAggr() = default;
128 };
129 DefaultedAggr da = { 42 } ;
130 
131 struct ExplicitDefaultedAggr {
132   int n;
133   explicit ExplicitDefaultedAggr() = default; // expected-note {{candidate}}
134   ExplicitDefaultedAggr(const ExplicitDefaultedAggr &) = default; // expected-note {{candidate}}
135   ExplicitDefaultedAggr(ExplicitDefaultedAggr &&) = default; // expected-note {{candidate}}
136 };
137 ExplicitDefaultedAggr eda = { 42 }; // expected-error {{no matching constructor}}
138 ExplicitDefaultedAggr eda2{};
139 
140 struct DefaultedBase {
141   int n;
142   DefaultedBase() = default;
143   DefaultedBase(DefaultedBase const&) = default;
144   DefaultedBase(DefaultedBase &&) = default;
145 };
146 
147 struct InheritingConstructors : DefaultedBase { // expected-note 3 {{candidate}}
148   using DefaultedBase::DefaultedBase;
149 };
150 InheritingConstructors ic = { 42 }; // expected-error {{no matching constructor}}
151 
152 struct NonInheritingConstructors : DefaultedBase {}; // expected-note 0+ {{candidate}}
153 NonInheritingConstructors nic = { 42 };
154 #if __cplusplus <= 201402L
155 // expected-error@-2 {{no matching constructor}}
156 #endif
157 
158 struct NonAggrBase {
NonAggrBaseNonAggrBase159   NonAggrBase(int) {}
160 };
161 struct HasNonAggrBase : NonAggrBase {}; // expected-note 0+ {{candidate}}
162 HasNonAggrBase hnab = {42};
163 #if __cplusplus <= 201402L
164 // expected-error@-2 {{no matching constructor}}
165 #endif
166