blob: bed526d399a030b61c8f8b9a78b1e4f15a41f0c5 [file] [log] [blame]
Douglas Gregor930d8b52009-01-30 22:09:00 +00001// RUN: clang -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{{initialization of non-aggregate type 'struct NonAggregate' with an initializer list}}
17
Sebastian Redl3cb06922009-02-07 19:52:04 +000018NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error 2 {{initialization of non-aggregate type 'struct NonAggregate' with an initializer list}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000019
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
Douglas Gregorb574e562009-01-30 22:26:29 +000037char cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000038
39// C++ [dcl.init.aggr]p7
40struct TooFew { int a; char* b; int c; };
41TooFew too_few = { 1, "asdf" }; // okay
42
Sebastian Redl3cb06922009-02-07 19:52:04 +000043struct NoDefaultConstructor { // expected-note 5 {{candidate function}}
44 NoDefaultConstructor(int); // expected-note 5 {{candidate function}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000045};
46struct TooFewError {
47 int a;
48 NoDefaultConstructor nodef;
49};
50TooFewError too_few_okay = { 1, 1 };
51TooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}}
52
53TooFewError too_few_okay2[2] = { 1, 1 };
54TooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}}
55
Sebastian Redl3cb06922009-02-07 19:52:04 +000056NoDefaultConstructor too_few_error3[3] = { }; // expected-error 3 {{no matching constructor}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000057
Douglas Gregor930d8b52009-01-30 22:09:00 +000058// C++ [dcl.init.aggr]p8
59struct Empty { };
60struct EmptyTest {
61 Empty s;
62 int i;
63} empty_test = { { }, 3 };
64
65EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}}
66
67struct NonEmpty {
68 int a;
69 Empty empty;
70};
71struct NonEmptyTest {
72 NonEmpty a, b;
73} non_empty_test = { { }, { } };
74
75// C++ [dcl.init.aggr]p9
76struct HasReference {
77 int i;
78 int &j; // expected-note{{uninitialized reference member is here}}
79};
80int global_int;
81HasReference r1 = { 1, global_int };
82HasReference r2 = { 1 } ; // expected-error{{initialization leaves reference member of type 'int &' uninitialized}}
83
84// C++ [dcl.init.aggr]p10
85// Note: the behavior here is identical to C
86int xs[2][2] = { 3, 1, 4, 2 };
87float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };
88
89// C++ [dcl.init.aggr]p11
90// Note: the behavior here is identical to C
91float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } };
92float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
93
94// C++ [dcl.init.aggr]p12
95struct A2 {
96 int i;
97 operator int *();
98};
99struct B2 {
100 A2 a1, a2;
101 int *z;
102};
103struct C2 {
104 operator A2();
105};
106struct D2 {
107 operator int();
108};
109A2 a2;
110C2 c2;
111D2 d2;
112B2 b2 = { 4, a2, a2 };
113B2 b2_2 = { 4, d2, 0 };
Douglas Gregor734d9862009-01-30 23:27:23 +0000114B2 b2_3 = { c2, a2, a2 };
Douglas Gregor930d8b52009-01-30 22:09:00 +0000115
116// C++ [dcl.init.aggr]p15:
117union u { int a; char* b; };
118u u1 = { 1 };
119u u2 = u1;
120u u3 = 1; // expected-error{{cannot initialize 'u3' with an rvalue of type 'int'}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000121u u4 = { 0, "asdf" }; // expected-error{{excess elements in union initializer}}
Douglas Gregor930d8b52009-01-30 22:09:00 +0000122u u5 = { "asdf" }; // expected-error{{incompatible type initializing 'char const [5]', expected 'int'}}