Clang Project

clang_source_code/test/CXX/dcl.decl/dcl.init/dcl.init.aggr/p1.cpp
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...
6struct Aggr {
7private:
8  static const int n;
9  void f();
10protected:
11  struct Inner { int m; };
12public:
13  bool &br;
14};
15bool b;
16Aggr ag = { b };
17
18// with no user-provided constructors, ...
19struct NonAggr1a { // expected-note 2 {{candidate constructor}}
20  NonAggr1a(int, int); // expected-note {{candidate constructor}}
21  int k;
22};
23NonAggr1a na1a = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr1a'}}
24
25struct NonAggr1b {
26  NonAggr1b(const NonAggr1b &); // expected-note {{candidate constructor}}
27  int k;
28};
29NonAggr1b 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.
33struct NonAggr2 {
34  int m = { 123 };
35};
36NonAggr2 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...
43struct NonAggr3 { // expected-note 3 {{candidate constructor}}
44private:
45  int n;
46};
47NonAggr3 na3 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr3'}}
48
49// or protected non-static data members, ...
50struct NonAggr4 { // expected-note 3 {{candidate constructor}}
51protected:
52  int n;
53};
54NonAggr4 na4 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr4'}}
55
56// [pre-C++1z] no base classes, ...
57struct NonAggr5 : Aggr {
58};
59NonAggr5 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
64template<typename...BaseList>
65struct MaybeAggr5a : BaseList... {};
66MaybeAggr5a<> ma5a0 = {}; // ok
67MaybeAggr5a<Aggr> ma5a1 = {}; // ok in C++17
68MaybeAggr5a<NonAggr2> m5a2 = {}; // ok, aggregate init in C++17, default ctor in C++11 and C++14
69MaybeAggr5a<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, ...
81struct NonAggr5b : virtual Aggr {}; // expected-note 3{{candidate}}
82NonAggr5b na5b = { b }; // expected-error {{no matching constructor}}
83struct NonAggr5c : NonAggr5b {}; // expected-note 3{{candidate}}
84NonAggr5c na5c = { b }; // expected-error {{no matching constructor}}
85struct NonAggr5d : protected Aggr {}; // expected-note 3{{candidate}}
86NonAggr5d na5d = { b }; // expected-error {{no matching constructor}}
87struct NonAggr5e : private Aggr {}; // expected-note 3{{candidate}}
88NonAggr5e na5e = { b }; // expected-error {{no matching constructor}}
89class NonAggr5f : Aggr {}; // expected-note 3{{candidate}}
90NonAggr5f na5f = { b }; // expected-error {{no matching constructor}}
91
92// [C++1z] (the base class need not itself be an aggregate)
93struct MaybeAggr5g : NonAggr1a {};
94MaybeAggr5g ma5g1 = { 1 };
95MaybeAggr5g ma5g2 = { {1, 2} };
96MaybeAggr5g 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.
108struct NonAggr6 { // expected-note 3 {{candidate constructor}}
109  virtual void f();
110  int n;
111};
112NonAggr6 na6 = { 42 }; // expected-error {{no matching constructor for initialization of 'NonAggr6'}}
113
114struct NonAggr7 : NonAggr6 { // expected-note 3 {{candidate constructor}}
115  int n;
116};
117NonAggr7 na7 = {{}, 42}; // expected-error {{no matching constructor for initialization of 'NonAggr7'}}
118
119struct 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};
129DefaultedAggr da = { 42 } ;
130
131struct 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};
137ExplicitDefaultedAggr eda = { 42 }; // expected-error {{no matching constructor}}
138ExplicitDefaultedAggr eda2{};
139
140struct DefaultedBase {
141  int n;
142  DefaultedBase() = default;
143  DefaultedBase(DefaultedBase const&) = default;
144  DefaultedBase(DefaultedBase &&) = default;
145};
146
147struct InheritingConstructors : DefaultedBase { // expected-note 3 {{candidate}}
148  using DefaultedBase::DefaultedBase;
149};
150InheritingConstructors ic = { 42 }; // expected-error {{no matching constructor}}
151
152struct NonInheritingConstructors : DefaultedBase {}; // expected-note 0+ {{candidate}}
153NonInheritingConstructors nic = { 42 };
154#if __cplusplus <= 201402L
155// expected-error@-2 {{no matching constructor}}
156#endif
157
158struct NonAggrBase {
159  NonAggrBase(int) {}
160};
161struct HasNonAggrBase : NonAggrBase {}; // expected-note 0+ {{candidate}}
162HasNonAggrBase hnab = {42};
163#if __cplusplus <= 201402L
164// expected-error@-2 {{no matching constructor}}
165#endif
166