xref: /llvm-project/clang/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp (revision 00c9dfdfd0941a52902d81a8e377b28037ee494d)
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 
3 // This must obviously come before the definition of std::initializer_list.
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 
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 
32     initializer_list() : __begin_(nullptr), __size_(0) {}
33 
34     size_t    size()  const {return __size_;}
35     const _E* begin() const {return __begin_;}
36     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 
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 
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 
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 
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 
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 
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 = {1, 2, 3}; // expected-warning{{at the end of the constructor}}
157   std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}}
158   haslist1();
159 };
160 
161 haslist1::haslist1()
162 : il{1, 2, 3} // expected-warning{{at the end of the constructor}}
163 {}
164 
165 namespace PR12119 {
166   // Deduction with nested initializer lists.
167   template<typename T> void f(std::initializer_list<T>);
168   template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
169 
170   void foo() {
171     f({0, {1}}); // expected-warning{{braces around scalar initializer}}
172     g({{0, 1}, {2, 3}});
173     std::initializer_list<int> il = {1, 2};
174     g({il, {2, 3}});
175   }
176 }
177 
178 namespace Decay {
179   template<typename T>
180   void f(std::initializer_list<T>) {
181     T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
182   }
183 
184   void g() {
185     f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
186 
187     auto x = { "A", "BB", "CCC" };
188     std::initializer_list<const char *> *il = &x;
189 
190     for( auto s : {"A", "BB", "CCC", "DDD"}) { }
191   }
192 }
193 
194 namespace PR12436 {
195   struct X {
196     template<typename T>
197     X(std::initializer_list<int>, T);
198   };
199 
200   X x({}, 17);
201 }
202 
203 namespace rdar11948732 {
204   template<typename T> struct X {};
205 
206   struct XCtorInit {
207     XCtorInit(std::initializer_list<X<int>>);
208   };
209 
210   void f(X<int> &xi) {
211     XCtorInit xc = { xi, xi };
212   }
213 }
214 
215 namespace PR14272 {
216   auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
217 }
218 
219 namespace initlist_of_array {
220   void f(std::initializer_list<int[2]>) {}
221   void f(std::initializer_list<int[2][2]>) = delete;
222   void h() {
223     f({{1,2},{3,4}});
224   }
225 }
226 
227 namespace init_list_deduction_failure {
228   void f();
229   void f(int);
230   // FIXME: It'd be nice to track that 'T' became a non-deduced context due to
231   // overload resolution failure for 'f'.
232   template<typename T> void g(std::initializer_list<T>);
233   // expected-note@-1 {{candidate template ignored: couldn't infer template argument 'T'}}
234   void h() {
235     g({f}); // expected-error {{no matching function for call to 'g'}}
236     g({f, h}); // ok
237   }
238 }
239 
240 namespace deleted_copy {
241   struct X {
242     X(int i) {}
243     X(const X& x) = delete; // expected-note {{here}}
244     void operator=(const X& x) = delete;
245   };
246 
247   std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}
248 }
249 
250 namespace RefVersusInitList {
251   struct S {};
252   void f(const S &) = delete;
253   void f(std::initializer_list<S>);
254   void g(S s) { f({S()}); }
255 }
256 
257 namespace PR18013 {
258   int f();
259   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')}}
260 }
261 
262 namespace DR1070 {
263   struct S {
264     S(std::initializer_list<int>);
265   };
266   S s[3] = { {1, 2, 3}, {4, 5} }; // ok
267   S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok
268 }
269 
270 namespace ListInitInstantiate {
271   struct A {
272     A(std::initializer_list<A>);
273     A(std::initializer_list<int>);
274   };
275   struct B : A {
276     B(int);
277   };
278   template<typename T> struct X {
279     X();
280     A a;
281   };
282   template<typename T> X<T>::X() : a{B{0}, B{1}} {}
283 
284   X<int> x;
285 
286   int f(const A&);
287   template<typename T> void g() { int k = f({0}); }
288   template void g<int>();
289 }
290 
291 namespace TemporaryInitListSourceRange_PR22367 {
292   struct A {
293     constexpr A() {}
294     A(std::initializer_list<int>); // expected-note {{here}}
295   };
296   constexpr int f(A) { return 0; }
297   constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
298       // The point of this test is to check that the caret points to
299       // 'std::initializer_list', not to '{0}'.
300       std::initializer_list // expected-note {{constructor}}
301       <int>
302       {0}
303       );
304 }
305 
306 namespace ParameterPackNestedInitializerLists_PR23904c3 {
307   template <typename ...T>
308   void f(std::initializer_list<std::initializer_list<T>> ...tt); // expected-note 2{{conflicting}} expected-note {{incomplete pack}}
309 
310   void foo() {
311     f({{0}}, {{'\0'}}); // ok, T = <int, char>
312     f({{0}, {'\0'}}); // expected-error {{no match}}
313     f({{0, '\0'}}); // expected-error {{no match}}
314 
315     f({{0}}, {{{}}}); // expected-error {{no match}}
316     f({{0}}, {{{}, '\0'}}); // ok, T = <int, char>
317     f({{0}, {{}}}); // ok, T = <int>
318     f({{0, {}}}); // ok, T = <int>
319   }
320 }
321 
322 namespace update_rbrace_loc_crash {
323   // We used to crash-on-invalid on this example when updating the right brace
324   // location.
325   template <typename T, T>
326   struct A {};
327   template <typename T, typename F, int... I>
328   std::initializer_list<T> ExplodeImpl(F p1, A<int, I...>) {
329     // expected-error@+1 {{reference to type 'const update_rbrace_loc_crash::Incomplete' could not bind to an rvalue of type 'void'}}
330     return {p1(I)...};
331   }
332   template <typename T, int N, typename F>
333   void Explode(F p1) {
334     // expected-note@+1 {{in instantiation of function template specialization}}
335     ExplodeImpl<T>(p1, A<int, N>());
336   }
337   class Incomplete;
338   struct ContainsIncomplete {
339     const Incomplete &obstacle;
340   };
341   void f() {
342     // expected-note@+1 {{in instantiation of function template specialization}}
343     Explode<ContainsIncomplete, 4>([](int) {});
344   }
345 }
346 
347 namespace no_conversion_after_auto_list_deduction {
348   // We used to deduce 'auto' == 'std::initializer_list<X>' here, and then
349   // incorrectly accept the declaration of 'x'.
350   struct X { using T = std::initializer_list<X> X::*; operator T(); };
351   auto X::*x = { X() }; // expected-error {{from initializer list}}
352 
353   struct Y { using T = std::initializer_list<Y>(*)(); operator T(); };
354   auto (*y)() = { Y() }; // expected-error {{from initializer list}}
355 }
356