blob: 6e2bee7e301d153284c6d22adbe2a844c4b31d54 [file] [log] [blame]
Sebastian Redlce7cd262011-05-20 21:07:01 +00001// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
2// XFAIL: *
3
4template <typename T, typename U>
5struct same_type { static const bool value = false; };
6template <typename T>
7struct same_type<T, T> { static const bool value = true; };
8
9namespace std {
10 typedef decltype(sizeof(int)) size_t;
11
12 // libc++'s implementation
13 template <class _E>
14 class initializer_list
15 {
16 const _E* __begin_;
17 size_t __size_;
18
19 initializer_list(const _E* __b, size_t __s)
20 : __begin_(__b),
21 __size_(__s)
22 {}
23
24 public:
25 typedef _E value_type;
26 typedef const _E& reference;
27 typedef const _E& const_reference;
28 typedef size_t size_type;
29
30 typedef const _E* iterator;
31 typedef const _E* const_iterator;
32
33 initializer_list() : __begin_(nullptr), __size_(0) {}
34
35 size_t size() const {return __size_;}
36 const _E* begin() const {return __begin_;}
37 const _E* end() const {return __begin_ + __size_;}
38 };
39}
40
41namespace integral {
42
43 void initialization() {
44 { const int a{}; static_assert(a == 0, ""); }
45 { const int a = {}; static_assert(a == 0, ""); }
46 { const int a{1}; static_assert(a == 1, ""); }
47 { const int a = {1}; static_assert(a == 1, ""); }
Sebastian Redldbef1bb2011-06-05 12:23:16 +000048 { const int a{1, 2}; } // expected-error {{excess elements}}
49 { const int a = {1, 2}; } // expected-error {{excess elements}}
50 { const short a{100000}; } // expected-error {{narrowing conversion}}
Sebastian Redlce7cd262011-05-20 21:07:01 +000051 { const short a = {100000}; } // expected-error {{narrowing conversion}}
52 }
53
54 int function_call() {
55 void takes_int(int);
56 takes_int({1});
57
58 int ar[10];
59 (void) ar[{1}]; // expected-error {{initializer list is illegal with the built-in index operator}}
60
61 return {1};
62 }
63
64 void inline_init() {
65 (void) int{1};
66 (void) new int{1};
67 }
68
69 void initializer_list() {
Sebastian Redl064e2362011-06-05 12:23:21 +000070 std::initializer_list<int> il = { 1, 2, 3 };
71 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
Sebastian Redlce7cd262011-05-20 21:07:01 +000072 auto l = {1, 2, 3, 4};
73 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
Sebastian Redl064e2362011-06-05 12:23:21 +000074 auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
Sebastian Redlce7cd262011-05-20 21:07:01 +000075
76 for (int i : {1, 2, 3, 4}) {}
77 }
78
79 struct A {
80 int i;
81 A() : i{1} {}
82 };
83
84}
85
86namespace objects {
87
Sebastian Redldc998b42011-07-14 19:08:01 +000088 struct X1 { X1(int); };
89 struct X2 { explicit X2(int); };
90
Sebastian Redl262b62b2011-06-05 12:23:08 +000091 template <int N>
Sebastian Redlce7cd262011-05-20 21:07:01 +000092 struct A {
Sebastian Redl262b62b2011-06-05 12:23:08 +000093 A() { static_assert(N == 0, ""); }
94 A(int, double) { static_assert(N == 1, ""); }
Sebastian Redl262b62b2011-06-05 12:23:08 +000095 A(std::initializer_list<int>) { static_assert(N == 3, ""); }
Sebastian Redlce7cd262011-05-20 21:07:01 +000096 };
97
Sebastian Redldc998b42011-07-14 19:08:01 +000098 template <int N>
99 struct D {
100 D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}}
101 D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}}
102 };
103
104 template <int N>
105 struct E {
106 E(int, int) { static_assert(N == 0, ""); }
107 E(X1, int) { static_assert(N == 1, ""); }
108 };
109
110 void overload_resolution() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000111 { A<0> a{}; }
112 { A<0> a = {}; }
Sebastian Redldc998b42011-07-14 19:08:01 +0000113 // Narrowing conversions don't affect viability. The next two choose
114 // the initializer_list constructor.
115 { A<3> a{1, 1.0}; } // expected-error {{narrowing conversion}}
116 { A<3> a = {1, 1.0}; } // expected-error {{narrowing conversion}}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000117 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
118 { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; }
119 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
120 { A<3> a{1, 2}; }
Sebastian Redldc998b42011-07-14 19:08:01 +0000121
122 { D<0> d{1, 2, 3}; }
123 { D<1> d{1.0, 2.0, 3.0}; }
124 { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}}
125
126 { E<0> e{1, 2}; }
127 }
128
129 void explicit_implicit() {
130 { X1 x{0}; }
131 { X1 x = {0}; }
132 { X2 x{0}; }
133 { X2 x = {0}; } // expected-error {{explicit}}
Sebastian Redlce7cd262011-05-20 21:07:01 +0000134 }
135
Sebastian Redl262b62b2011-06-05 12:23:08 +0000136 struct C {
137 C();
138 C(int, double);
139 C(int, int);
140 C(std::initializer_list<int>);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000141
Sebastian Redl262b62b2011-06-05 12:23:08 +0000142 int operator[](C);
143 };
144
145 C function_call() {
146 void takes_C(C);
147 takes_C({1, 1.0});
148
149 C c;
150 c[{1, 1.0}];
Sebastian Redlce7cd262011-05-20 21:07:01 +0000151
152 return {1, 1.0};
153 }
154
155 void inline_init() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000156 (void) A<1>{1, 1.0};
157 (void) new A<1>{1, 1.0};
Sebastian Redlce7cd262011-05-20 21:07:01 +0000158 }
159
160 struct B {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000161 B(C, int, C);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000162 };
163
164 void nested_init() {
165 B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
166 }
167}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000168
169namespace litb {
170
171 // invalid
172 struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}}
173
174 // invalid
175 int a({0}); // expected-error {{}}
176
177 // invalid
178 int const &b({0}); // expected-error {{}}
179
180 struct C { explicit C(int, int); C(int, long); };
181
182 // invalid
183 C c({1, 2}); // expected-error {{}}
184
185 // valid (by copy constructor).
186 C d({1, 2L}); // expected-error {{}}
187
188 // valid
189 C e{1, 2};
190
191 struct B {
192 template<typename ...T>
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000193 B(std::initializer_list<int>, T ...);
Sebastian Redl262b62b2011-06-05 12:23:08 +0000194 };
195
196 // invalid (the first phase only considers init-list ctors)
197 // (for the second phase, no constructor is viable)
198 B f{1, 2, 3};
199
200 // valid (T deduced to <>).
201 B g({1, 2, 3});
202
203}
Sebastian Redldc998b42011-07-14 19:08:01 +0000204
205namespace aggregate {
206 // Direct list initialization does NOT allow braces to be elided!
207 struct S {
208 int ar[2];
209 struct T {
210 int i1;
211 int i2;
212 } t;
213 struct U {
214 int i1;
215 } u[2];
216 struct V {
217 int var[2];
218 } v;
219 };
220
221 void test() {
222 S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; // no-error
223 S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced
224 S s3{ 1, 2, 3, 4, 5, 6 }; // xpected-error
225 S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // xpected-error
226 S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // xpected-error
227 }
228}