dcl_init_aggr.cpp revision 7c2342dd4c9947806842e5aca3d2bb2e542853c9
1// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
2// C++ [dcl.init.aggr]p2
3struct A {
4  int x;
5  struct B {
6    int i;
7    int j;
8  } b;
9} a1 = { 1, { 2, 3 } };
10
11struct NonAggregate {
12  NonAggregate();
13
14  int a, b;
15};
16NonAggregate non_aggregate_test = { 1, 2 }; // expected-error{{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}}
17
18NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error 2 {{initialization of non-aggregate type 'NonAggregate' with an initializer list}}
19
20
21// C++ [dcl.init.aggr]p3
22A a_init = A();
23
24// C++ [dcl.init.aggr]p4
25int x[] = { 1, 3, 5 };
26int x_sizecheck[(sizeof(x) / sizeof(int)) == 3? 1 : -1];
27int x2[] = { }; // expected-warning{{zero size arrays are an extension}}
28
29// C++ [dcl.init.aggr]p5
30struct StaticMemberTest {
31  int i;
32  static int s;
33  int *j;
34} smt = { 1, &smt.i };
35
36// C++ [dcl.init.aggr]p6
37char cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}}
38
39// C++ [dcl.init.aggr]p7
40struct TooFew { int a; char* b; int c; };
41TooFew too_few = { 1, "asdf" }; // expected-warning{{conversion from string literal to 'char *' is deprecated}}
42
43struct NoDefaultConstructor { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} \
44                              // expected-note{{declared here}}
45  NoDefaultConstructor(int); // expected-note 3 {{candidate constructor}}
46};
47struct TooFewError { // expected-error{{implicit default constructor for}}
48  int a;
49  NoDefaultConstructor nodef; // expected-note{{member is declared here}}
50};
51TooFewError too_few_okay = { 1, 1 };
52TooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}}
53
54TooFewError too_few_okay2[2] = { 1, 1 }; // expected-note{{implicit default constructor for 'TooFewError' first required here}}
55TooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}}
56
57NoDefaultConstructor too_few_error3[3] = { }; // expected-error {{no matching constructor}}
58
59// C++ [dcl.init.aggr]p8
60struct Empty { };
61struct EmptyTest {
62  Empty s;
63  int i;
64} empty_test = { { }, 3 };
65
66EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}}
67
68struct NonEmpty {
69  int a;
70  Empty empty;
71};
72struct NonEmptyTest {
73  NonEmpty a, b;
74} non_empty_test = { { }, { } };
75
76// C++ [dcl.init.aggr]p9
77struct HasReference {
78  int i;
79  int &j; // expected-note{{uninitialized reference member is here}}
80};
81int global_int;
82HasReference r1 = { 1, global_int };
83HasReference r2 = { 1 } ; // expected-error{{initialization leaves reference member of type 'int &' uninitialized}}
84
85// C++ [dcl.init.aggr]p10
86// Note: the behavior here is identical to C
87int xs[2][2] = { 3, 1, 4, 2 };
88float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };
89
90// C++ [dcl.init.aggr]p11
91// Note: the behavior here is identical to C
92float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } };
93float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
94
95// C++ [dcl.init.aggr]p12
96struct A2 {
97  int i;
98  operator int *();
99};
100struct B2 {
101  A2 a1, a2;
102  int *z;
103};
104struct C2 {
105  operator A2();
106};
107struct D2 {
108  operator int();
109};
110A2 a2;
111C2 c2;
112D2 d2;
113B2 b2 = { 4, a2, a2 };
114B2 b2_2 = { 4, d2, 0 };
115B2 b2_3 = { c2, a2, a2 };
116
117// C++ [dcl.init.aggr]p15:
118union u { int a; char* b; }; // expected-note{{candidate constructor (the implicit copy constructor)}}
119u u1 = { 1 };
120u u2 = u1;
121u u3 = 1; // expected-error{{no viable conversion}}
122u u4 = { 0, "asdf" };  // expected-error{{excess elements in union initializer}}
123u u5 = { "asdf" }; // expected-error{{cannot initialize a member subobject of type 'int' with an lvalue of type 'char const [5]'}}
124