blob: f53ac6dff930bbf4ded467feff0bb4941f3b4c9c [file] [log] [blame]
Sebastian Redlc2235182011-10-16 18:19:28 +00001// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
2
Sebastian Redlcc7a6482011-11-01 15:53:09 +00003struct one { char c[1]; };
4struct two { char c[2]; };
5
Sebastian Redlc2235182011-10-16 18:19:28 +00006namespace aggregate {
7 // Direct list initialization does NOT allow braces to be elided!
8 struct S {
9 int ar[2];
10 struct T {
11 int i1;
12 int i2;
13 } t;
14 struct U {
15 int i1;
16 } u[2];
17 struct V {
18 int var[2];
19 } v;
20 };
21
Sebastian Redlcc7a6482011-11-01 15:53:09 +000022 void bracing() {
Sebastian Redlc2235182011-10-16 18:19:28 +000023 S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; // no-error
24 S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
25 S s3{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
26 S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
27 S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
28 }
Sebastian Redlcc7a6482011-11-01 15:53:09 +000029
Sebastian Redl6dc00f62012-02-12 18:41:05 +000030 void bracing_new() {
31 new S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
32 new S{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
33 new S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
34 new S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
35 }
36
Sebastian Redl20ff0e22012-02-13 19:55:43 +000037 void bracing_construct() {
38 (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
39 (void) S{ 1, 2, 3, 4, 5, 6 }; // expected-error 5 {{cannot omit braces}}
40 (void) S{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // expected-error 2 {{cannot omit braces}}
41 (void) S{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // expected-error {{cannot omit braces}}
42 }
43
Sebastian Redlcc7a6482011-11-01 15:53:09 +000044 struct String {
45 String(const char*);
46 };
47
48 struct A {
49 int m1;
50 int m2;
51 };
52
53 void function_call() {
54 void takes_A(A);
55 takes_A({1, 2});
56 }
57
58 struct B {
59 int m1;
60 String m2;
61 };
62
63 void overloaded_call() {
64 one overloaded(A);
65 two overloaded(B);
66
67 static_assert(sizeof(overloaded({1, 2})) == sizeof(one), "bad overload");
68 static_assert(sizeof(overloaded({1, "two"})) == sizeof(two),
69 "bad overload");
70 // String is not default-constructible
71 static_assert(sizeof(overloaded({1})) == sizeof(one), "bad overload");
72 }
Sebastian Redl3a45c0e2012-02-12 16:37:36 +000073
Richard Smith9f02d6d2012-02-16 00:54:02 +000074 struct C { int a[2]; C():a({1, 2}) { } }; // expected-error {{parenthesized initialization of a member array is a GNU extension}}
Sebastian Redlc2235182011-10-16 18:19:28 +000075}
Eli Friedmanc60ccf52012-02-29 00:00:28 +000076
77namespace array_explicit_conversion {
78 typedef int test1[2];
79 typedef int test2[];
80 template<int x> struct A { int a[x]; }; // expected-error {{'a' declared as an array with a negative size}}
81 typedef A<1> test3[];
82 typedef A<-1> test4[];
83 void f() {
84 (void)test1{1};
85 (void)test2{1};
86 (void)test3{{{1}}};
87 (void)test4{{{1}}}; // expected-note {{in instantiation of template class 'array_explicit_conversion::A<-1>' requested here}}
88 }
89}
Richard Smith20599392012-07-07 08:35:56 +000090
91namespace sub_constructor {
92 struct DefaultConstructor { // expected-note 2 {{not viable}}
93 DefaultConstructor(); // expected-note {{not viable}}
94 int x;
95 };
96 struct NoDefaultConstructor1 { // expected-note 2 {{not viable}}
97 NoDefaultConstructor1(int); // expected-note {{not viable}}
98 int x;
99 };
100 struct NoDefaultConstructor2 { // expected-note 4 {{not viable}}
101 NoDefaultConstructor2(int,int); // expected-note 2 {{not viable}}
102 int x;
103 };
104
105 struct Aggr {
106 DefaultConstructor a;
107 NoDefaultConstructor1 b;
108 NoDefaultConstructor2 c;
109 };
110
111 Aggr ok1 { {}, {0} , {0,0} };
112 Aggr ok2 = { {}, {0} , {0,0} };
113 Aggr too_many { {0} , {0} , {0,0} }; // expected-error {{no matching constructor for initialization}}
114 Aggr too_few { {} , {0} , {0} }; // expected-error {{no matching constructor for initialization}}
115 Aggr invalid { {} , {&ok1} , {0,0} }; // expected-error {{no matching constructor for initialization}}
116 NoDefaultConstructor2 array_ok[] = { {0,0} , {0,1} };
117 NoDefaultConstructor2 array_error[] = { {0,0} , {0} }; // expected-error {{no matching constructor for initialization}}
Richard Smith2801d9a2012-12-09 06:48:56 +0000118}
119
120namespace multidimensional_array {
121 void g(const int (&)[2][2]) {}
122 void g(const int (&)[2][2][2]) = delete;
123
124 void h() {
125 g({{1,2},{3,4}});
126 }
127}
Richard Smith3fa3fea2013-02-02 02:14:45 +0000128
129namespace array_addressof {
130 using T = int[5];
131 T *p = &T{1,2,3,4,5}; // expected-error {{taking the address of a temporary object of type 'T' (aka 'int [5]')}}
132}