1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
3 // This must obviously come before the definition of std::initializer_list.
missing_initializerlist()4 void missing_initializerlist() {
5 auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}
6 }
7
8 namespace std {
9 typedef decltype(sizeof(int)) size_t;
10
11 // libc++'s implementation
12 template <class _E>
13 class initializer_list
14 {
15 const _E* __begin_;
16 size_t __size_;
17
initializer_list(const _E * __b,size_t __s)18 initializer_list(const _E* __b, size_t __s)
19 : __begin_(__b),
20 __size_(__s)
21 {}
22
23 public:
24 typedef _E value_type;
25 typedef const _E& reference;
26 typedef const _E& const_reference;
27 typedef size_t size_type;
28
29 typedef const _E* iterator;
30 typedef const _E* const_iterator;
31
initializer_list()32 initializer_list() : __begin_(nullptr), __size_(0) {}
33
size() const34 size_t size() const {return __size_;}
begin() const35 const _E* begin() const {return __begin_;}
end() const36 const _E* end() const {return __begin_ + __size_;}
37 };
38 }
39
40 template <typename T, typename U>
41 struct same_type { static const bool value = false; };
42 template <typename T>
43 struct same_type<T, T> { static const bool value = true; };
44
45 struct one { char c[1]; };
46 struct two { char c[2]; };
47
48 struct A {
49 int a, b;
50 };
51
52 struct B {
53 B();
54 B(int, int);
55 };
56
simple_list()57 void simple_list() {
58 std::initializer_list<int> il = { 1, 2, 3 };
59 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
60 std::initializer_list<A> al = { {1, 2}, {2, 3}, {3, 4} };
61 std::initializer_list<B> bl = { {1, 2}, {2, 3}, {} };
62 }
63
function_call()64 void function_call() {
65 void f(std::initializer_list<int>);
66 f({1, 2, 3});
67
68 void g(std::initializer_list<B>);
69 g({ {1, 2}, {2, 3}, {} });
70 }
71
72 struct C {
73 C(int);
74 };
75
76 struct D {
77 D();
78 operator int();
79 operator C();
80 };
81
overloaded_call()82 void overloaded_call() {
83 one overloaded(std::initializer_list<int>);
84 two overloaded(std::initializer_list<B>);
85
86 static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one), "bad overload");
87 static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two), "bad overload");
88
89 void ambiguous(std::initializer_list<A>); // expected-note {{candidate}}
90 void ambiguous(std::initializer_list<B>); // expected-note {{candidate}}
91 ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}}
92
93 one ov2(std::initializer_list<int>); // expected-note {{candidate}}
94 two ov2(std::initializer_list<C>); // expected-note {{candidate}}
95 // Worst sequence to int is identity, whereas to C it's user-defined.
96 static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one), "bad overload");
97 // But here, user-defined is worst in both cases.
98 ov2({1, 2, D()}); // expected-error {{ambiguous}}
99 }
100
101 template <typename T>
102 T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
103 template <typename T>
104 T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
105
106 template<typename T, typename U> struct pair { pair(...); };
107 template<typename T> void deduce_pairs(std::initializer_list<pair<T, typename T::type>>);
108 // expected-note@-1 {{deduced type 'pair<[...], typename WithIntType::type>' of element of 1st parameter does not match adjusted type 'pair<[...], float>' of element of argument [with T = WithIntType]}}
109 struct WithIntType { typedef int type; };
110
111 template<typename ...T> void deduce_after_init_list_in_pack(void (*)(T...), T...); // expected-note {{<int, int> vs. <(no value), double>}}
112
argument_deduction()113 void argument_deduction() {
114 static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction");
115 static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction");
116
117 deduce({1, 2.0}); // expected-error {{no matching function}}
118
119 static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction");
120 static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction");
121
122 deduce_ref({1, 2.0}); // expected-error {{no matching function}}
123
124 pair<WithIntType, int> pi;
125 pair<WithIntType, float> pf;
126 deduce_pairs({pi, pi, pi}); // ok
127 deduce_pairs({pi, pf, pi}); // expected-error {{no matching function}}
128
129 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0);
130 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0.0); // expected-error {{no matching function}}
131 }
132
auto_deduction()133 void auto_deduction() {
134 auto l = {1, 2, 3, 4};
135 auto l2 {1, 2, 3, 4}; // expected-error {{initializer for variable 'l2' with type 'auto' contains multiple expressions}}
136 auto l3 {1};
137 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
138 static_assert(same_type<decltype(l3), int>::value, "");
139 auto bl = {1, 2.0}; // expected-error {{deduced conflicting types ('int' vs 'double') for initializer list element type}}
140
141 void f1(int), f1(float), f2(int), f3(float);
142 auto fil = {f1, f2};
143 auto ffl = {f1, f3};
144 auto fl = {f1, f2, f3}; // expected-error {{deduced conflicting types ('void (*)(int)' vs 'void (*)(float)') for initializer list element type}}
145
146 for (int i : {1, 2, 3, 4}) {}
147 for (int j : {1.0, 2.0, 3.0f, 4.0}) {} // expected-error {{deduced conflicting types ('double' vs 'float') for initializer list element type}}
148 }
149
dangle()150 void dangle() {
151 new auto{1, 2, 3}; // expected-error {{new expression for type 'auto' contains multiple constructor arguments}}
152 new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
153 }
154
155 struct haslist1 {
156 std::initializer_list<int> il // expected-note {{declared here}}
157 = {1, 2, 3}; // ok, unused
158 std::initializer_list<int> jl{1, 2, 3}; // expected-note {{default member init}}
159 haslist1();
160 };
161
haslist1()162 haslist1::haslist1() // expected-error {{backing array for 'std::initializer_list' member 'jl' is a temporary object}}
163 : il{1, 2, 3} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
164 {}
165
166 namespace PR12119 {
167 // Deduction with nested initializer lists.
168 template<typename T> void f(std::initializer_list<T>);
169 template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
170
foo()171 void foo() {
172 f({0, {1}}); // expected-warning{{braces around scalar initializer}}
173 g({{0, 1}, {2, 3}});
174 std::initializer_list<int> il = {1, 2};
175 g({il, {2, 3}});
176 }
177 }
178
179 namespace Decay {
180 template<typename T>
f(std::initializer_list<T>)181 void f(std::initializer_list<T>) {
182 T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
183 }
184
g()185 void g() {
186 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
187
188 auto x = { "A", "BB", "CCC" };
189 std::initializer_list<const char *> *il = &x;
190
191 for( auto s : {"A", "BB", "CCC", "DDD"}) { }
192 }
193 }
194
195 namespace PR12436 {
196 struct X {
197 template<typename T>
198 X(std::initializer_list<int>, T);
199 };
200
201 X x({}, 17);
202 }
203
204 namespace rdar11948732 {
205 template<typename T> struct X {};
206
207 struct XCtorInit {
208 XCtorInit(std::initializer_list<X<int>>);
209 };
210
f(X<int> & xi)211 void f(X<int> &xi) {
212 XCtorInit xc = { xi, xi };
213 }
214 }
215
216 namespace PR14272 {
217 auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
218 }
219
220 namespace initlist_of_array {
f(std::initializer_list<int[2]>)221 void f(std::initializer_list<int[2]>) {}
222 void f(std::initializer_list<int[2][2]>) = delete;
h()223 void h() {
224 f({{1,2},{3,4}});
225 }
226 }
227
228 namespace init_list_deduction_failure {
229 void f();
230 void f(int);
231 // FIXME: It'd be nice to track that 'T' became a non-deduced context due to
232 // overload resolution failure for 'f'.
233 template<typename T> void g(std::initializer_list<T>);
234 // expected-note@-1 {{candidate template ignored: couldn't infer template argument 'T'}}
h()235 void h() {
236 g({f}); // expected-error {{no matching function for call to 'g'}}
237 g({f, h}); // ok
238 }
239 }
240
241 namespace deleted_copy {
242 struct X {
Xdeleted_copy::X243 X(int i) {}
244 X(const X& x) = delete; // expected-note {{here}}
245 void operator=(const X& x) = delete;
246 };
247
248 std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}
249 }
250
251 namespace RefVersusInitList {
252 struct S {};
253 void f(const S &) = delete;
254 void f(std::initializer_list<S>);
g(S s)255 void g(S s) { f({S()}); }
256 }
257
258 namespace PR18013 {
259 int f();
260 std::initializer_list<long (*)()> x = {f}; // expected-error {{cannot initialize an array element of type 'long (*const)()' with an lvalue of type 'int ()': different return type ('long' vs 'int')}}
261 }
262
263 namespace DR1070 {
264 struct S {
265 S(std::initializer_list<int>);
266 };
267 S s[3] = { {1, 2, 3}, {4, 5} }; // ok
268 S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok
269 }
270
271 namespace ListInitInstantiate {
272 struct A {
273 A(std::initializer_list<A>);
274 A(std::initializer_list<int>);
275 };
276 struct B : A {
277 B(int);
278 };
279 template<typename T> struct X {
280 X();
281 A a;
282 };
X()283 template<typename T> X<T>::X() : a{B{0}, B{1}} {}
284
285 X<int> x;
286
287 int f(const A&);
g()288 template<typename T> void g() { int k = f({0}); }
289 template void g<int>();
290 }
291
292 namespace TemporaryInitListSourceRange_PR22367 {
293 struct A {
ATemporaryInitListSourceRange_PR22367::A294 constexpr A() {}
295 A(std::initializer_list<int>); // expected-note {{here}}
296 };
f(A)297 constexpr int f(A) { return 0; }
298 constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
299 // The point of this test is to check that the caret points to
300 // 'std::initializer_list', not to '{0}'.
301 std::initializer_list // expected-note {{constructor}}
302 <int>
303 {0}
304 );
305 }
306
307 namespace ParameterPackNestedInitializerLists_PR23904c3 {
308 template <typename ...T>
309 void f(std::initializer_list<std::initializer_list<T>> ...tt); // expected-note 2{{conflicting}} expected-note {{incomplete pack}}
310
foo()311 void foo() {
312 f({{0}}, {{'\0'}}); // ok, T = <int, char>
313 f({{0}, {'\0'}}); // expected-error {{no match}}
314 f({{0, '\0'}}); // expected-error {{no match}}
315
316 f({{0}}, {{{}}}); // expected-error {{no match}}
317 f({{0}}, {{{}, '\0'}}); // ok, T = <int, char>
318 f({{0}, {{}}}); // ok, T = <int>
319 f({{0, {}}}); // ok, T = <int>
320 }
321 }
322
323 namespace update_rbrace_loc_crash {
324 // We used to crash-on-invalid on this example when updating the right brace
325 // location.
326 template <typename T, T>
327 struct A {};
328 template <typename T, typename F, int... I>
ExplodeImpl(F p1,A<int,I...>)329 std::initializer_list<T> ExplodeImpl(F p1, A<int, I...>) {
330 // expected-error@+1 {{reference to type 'const update_rbrace_loc_crash::Incomplete' could not bind to an rvalue of type 'void'}}
331 return {p1(I)...};
332 }
333 template <typename T, int N, typename F>
Explode(F p1)334 void Explode(F p1) {
335 // expected-note@+1 {{in instantiation of function template specialization}}
336 ExplodeImpl<T>(p1, A<int, N>());
337 }
338 class Incomplete;
339 struct ContainsIncomplete {
340 const Incomplete &obstacle;
341 };
f()342 void f() {
343 // expected-note@+1 {{in instantiation of function template specialization}}
344 Explode<ContainsIncomplete, 4>([](int) {});
345 }
346 }
347
348 namespace no_conversion_after_auto_list_deduction {
349 // We used to deduce 'auto' == 'std::initializer_list<X>' here, and then
350 // incorrectly accept the declaration of 'x'.
351 struct X { using T = std::initializer_list<X> X::*; operator T(); };
352 auto X::*x = { X() }; // expected-error {{from initializer list}}
353
354 struct Y { using T = std::initializer_list<Y>(*)(); operator T(); };
355 auto (*y)() = { Y() }; // expected-error {{from initializer list}}
356 }
357