xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/cxx0x-initializer-aggregates.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc struct one { char c[1]; };
4*f4a2713aSLionel Sambuc struct two { char c[2]; };
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc namespace aggregate {
7*f4a2713aSLionel Sambuc   struct S {
8*f4a2713aSLionel Sambuc     int ar[2];
9*f4a2713aSLionel Sambuc     struct T {
10*f4a2713aSLionel Sambuc       int i1;
11*f4a2713aSLionel Sambuc       int i2;
12*f4a2713aSLionel Sambuc     } t;
13*f4a2713aSLionel Sambuc     struct U {
14*f4a2713aSLionel Sambuc       int i1;
15*f4a2713aSLionel Sambuc     } u[2];
16*f4a2713aSLionel Sambuc     struct V {
17*f4a2713aSLionel Sambuc       int var[2];
18*f4a2713aSLionel Sambuc     } v;
19*f4a2713aSLionel Sambuc   };
20*f4a2713aSLionel Sambuc 
bracing()21*f4a2713aSLionel Sambuc   void bracing() {
22*f4a2713aSLionel Sambuc     S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 };
23*f4a2713aSLionel Sambuc     S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } };
24*f4a2713aSLionel Sambuc     S s3{ 1, 2, 3, 4, 5, 6 };
25*f4a2713aSLionel Sambuc     S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } };
26*f4a2713aSLionel Sambuc     S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} };
27*f4a2713aSLionel Sambuc   }
28*f4a2713aSLionel Sambuc 
bracing_new()29*f4a2713aSLionel Sambuc   void bracing_new() {
30*f4a2713aSLionel Sambuc     new S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } };
31*f4a2713aSLionel Sambuc     new S{ 1, 2, 3, 4, 5, 6 };
32*f4a2713aSLionel Sambuc     new S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } };
33*f4a2713aSLionel Sambuc     new S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} };
34*f4a2713aSLionel Sambuc   }
35*f4a2713aSLionel Sambuc 
bracing_construct()36*f4a2713aSLionel Sambuc   void bracing_construct() {
37*f4a2713aSLionel Sambuc     (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } };
38*f4a2713aSLionel Sambuc     (void) S{ 1, 2, 3, 4, 5, 6 };
39*f4a2713aSLionel Sambuc     (void) S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } };
40*f4a2713aSLionel Sambuc     (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} };
41*f4a2713aSLionel Sambuc   }
42*f4a2713aSLionel Sambuc 
43*f4a2713aSLionel Sambuc   struct String {
44*f4a2713aSLionel Sambuc     String(const char*);
45*f4a2713aSLionel Sambuc   };
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc   struct A {
48*f4a2713aSLionel Sambuc     int m1;
49*f4a2713aSLionel Sambuc     int m2;
50*f4a2713aSLionel Sambuc   };
51*f4a2713aSLionel Sambuc 
function_call()52*f4a2713aSLionel Sambuc   void function_call() {
53*f4a2713aSLionel Sambuc     void takes_A(A);
54*f4a2713aSLionel Sambuc     takes_A({1, 2});
55*f4a2713aSLionel Sambuc   }
56*f4a2713aSLionel Sambuc 
57*f4a2713aSLionel Sambuc   struct B {
58*f4a2713aSLionel Sambuc     int m1;
59*f4a2713aSLionel Sambuc     String m2;
60*f4a2713aSLionel Sambuc   };
61*f4a2713aSLionel Sambuc 
overloaded_call()62*f4a2713aSLionel Sambuc   void overloaded_call() {
63*f4a2713aSLionel Sambuc     one overloaded(A);
64*f4a2713aSLionel Sambuc     two overloaded(B);
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc     static_assert(sizeof(overloaded({1, 2})) == sizeof(one), "bad overload");
67*f4a2713aSLionel Sambuc     static_assert(sizeof(overloaded({1, "two"})) == sizeof(two),
68*f4a2713aSLionel Sambuc       "bad overload");
69*f4a2713aSLionel Sambuc     // String is not default-constructible
70*f4a2713aSLionel Sambuc     static_assert(sizeof(overloaded({1})) == sizeof(one), "bad overload");
71*f4a2713aSLionel Sambuc   }
72*f4a2713aSLionel Sambuc 
Caggregate::C73*f4a2713aSLionel Sambuc   struct C { int a[2]; C():a({1, 2}) { } }; // expected-error {{parenthesized initialization of a member array is a GNU extension}}
74*f4a2713aSLionel Sambuc }
75*f4a2713aSLionel Sambuc 
76*f4a2713aSLionel Sambuc namespace array_explicit_conversion {
77*f4a2713aSLionel Sambuc   typedef int test1[2];
78*f4a2713aSLionel Sambuc   typedef int test2[];
79*f4a2713aSLionel Sambuc   template<int x> struct A { int a[x]; }; // expected-error {{'a' declared as an array with a negative size}}
80*f4a2713aSLionel Sambuc   typedef A<1> test3[];
81*f4a2713aSLionel Sambuc   typedef A<-1> test4[];
f()82*f4a2713aSLionel Sambuc   void f() {
83*f4a2713aSLionel Sambuc     (void)test1{1};
84*f4a2713aSLionel Sambuc     (void)test2{1};
85*f4a2713aSLionel Sambuc     (void)test3{{{1}}};
86*f4a2713aSLionel Sambuc     (void)test4{{{1}}}; // expected-note {{in instantiation of template class 'array_explicit_conversion::A<-1>' requested here}}
87*f4a2713aSLionel Sambuc   }
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc namespace sub_constructor {
91*f4a2713aSLionel Sambuc   struct DefaultConstructor { // expected-note 2 {{not viable}}
92*f4a2713aSLionel Sambuc     DefaultConstructor(); // expected-note  {{not viable}}
93*f4a2713aSLionel Sambuc     int x;
94*f4a2713aSLionel Sambuc   };
95*f4a2713aSLionel Sambuc   struct NoDefaultConstructor1 { // expected-note 2 {{not viable}}
96*f4a2713aSLionel Sambuc     NoDefaultConstructor1(int); // expected-note {{not viable}}
97*f4a2713aSLionel Sambuc     int x;
98*f4a2713aSLionel Sambuc   };
99*f4a2713aSLionel Sambuc   struct NoDefaultConstructor2 {  // expected-note 4 {{not viable}}
100*f4a2713aSLionel Sambuc     NoDefaultConstructor2(int,int); // expected-note 2 {{not viable}}
101*f4a2713aSLionel Sambuc     int x;
102*f4a2713aSLionel Sambuc   };
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc   struct Aggr {
105*f4a2713aSLionel Sambuc     DefaultConstructor a;
106*f4a2713aSLionel Sambuc     NoDefaultConstructor1 b;
107*f4a2713aSLionel Sambuc     NoDefaultConstructor2 c;
108*f4a2713aSLionel Sambuc   };
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc   Aggr ok1 { {}, {0} , {0,0} };
111*f4a2713aSLionel Sambuc   Aggr ok2 = { {}, {0} , {0,0} };
112*f4a2713aSLionel Sambuc   Aggr too_many { {0} , {0} , {0,0} }; // expected-error {{no matching constructor for initialization}}
113*f4a2713aSLionel Sambuc   Aggr too_few { {} , {0} , {0} }; // expected-error {{no matching constructor for initialization}}
114*f4a2713aSLionel Sambuc   Aggr invalid { {} , {&ok1} , {0,0} }; // expected-error {{no matching constructor for initialization}}
115*f4a2713aSLionel Sambuc   NoDefaultConstructor2 array_ok[] = { {0,0} , {0,1} };
116*f4a2713aSLionel Sambuc   NoDefaultConstructor2 array_error[] = { {0,0} , {0} }; // expected-error {{no matching constructor for initialization}}
117*f4a2713aSLionel Sambuc }
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc namespace multidimensional_array {
g(const int (&)[2][2])120*f4a2713aSLionel Sambuc   void g(const int (&)[2][2]) {}
121*f4a2713aSLionel Sambuc   void g(const int (&)[2][2][2]) = delete;
122*f4a2713aSLionel Sambuc 
h()123*f4a2713aSLionel Sambuc   void h() {
124*f4a2713aSLionel Sambuc     g({{1,2},{3,4}});
125*f4a2713aSLionel Sambuc   }
126*f4a2713aSLionel Sambuc }
127*f4a2713aSLionel Sambuc 
128*f4a2713aSLionel Sambuc namespace array_addressof {
129*f4a2713aSLionel Sambuc   using T = int[5];
130*f4a2713aSLionel Sambuc   T *p = &T{1,2,3,4,5}; // expected-error {{taking the address of a temporary object of type 'T' (aka 'int [5]')}}
131*f4a2713aSLionel Sambuc }
132