1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
3// This must obviously come before the definition of std::initializer_list.
4void missing_initializerlist() {
5  auto l = {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}
6}
7
8namespace 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
40template <typename T, typename U>
41struct same_type { static const bool value = false; };
42template <typename T>
43struct same_type<T, T> { static const bool value = true; };
44
45struct one { char c[1]; };
46struct two { char c[2]; };
47
48struct A {
49  int a, b;
50};
51
52struct B {
53  B();
54  B(int, int);
55};
56
57void 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
64void 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
72struct C {
73  C(int);
74};
75
76struct D {
77  D();
78  operator int();
79  operator C();
80};
81
82void 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
101template <typename T>
102T deduce(std::initializer_list<T>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
103template <typename T>
104T deduce_ref(const std::initializer_list<T>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
105
106void argument_deduction() {
107  static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction");
108  static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction");
109
110  deduce({1, 2.0}); // expected-error {{no matching function}}
111
112  static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction");
113  static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction");
114
115  deduce_ref({1, 2.0}); // expected-error {{no matching function}}
116}
117
118void auto_deduction() {
119  auto l = {1, 2, 3, 4};
120  auto l2 {1, 2, 3, 4}; // expected-warning {{will change meaning in a future version of Clang}}
121  static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
122  auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
123
124  for (int i : {1, 2, 3, 4}) {}
125}
126
127void dangle() {
128  new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}}
129  new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
130}
131
132struct haslist1 {
133  std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}}
134  std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}}
135  haslist1();
136};
137
138haslist1::haslist1()
139: il{1, 2, 3} // expected-warning{{at the end of the constructor}}
140{}
141
142namespace PR12119 {
143  // Deduction with nested initializer lists.
144  template<typename T> void f(std::initializer_list<T>);
145  template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
146
147  void foo() {
148    f({0, {1}}); // expected-warning{{braces around scalar initializer}}
149    g({{0, 1}, {2, 3}});
150    std::initializer_list<int> il = {1, 2};
151    g({il, {2, 3}});
152  }
153}
154
155namespace Decay {
156  template<typename T>
157  void f(std::initializer_list<T>) {
158    T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
159  }
160
161  void g() {
162    f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
163
164    auto x = { "A", "BB", "CCC" };
165    std::initializer_list<const char *> *il = &x;
166
167    for( auto s : {"A", "BB", "CCC", "DDD"}) { }
168  }
169}
170
171namespace PR12436 {
172  struct X {
173    template<typename T>
174    X(std::initializer_list<int>, T);
175  };
176
177  X x({}, 17);
178}
179
180namespace rdar11948732 {
181  template<typename T> struct X {};
182
183  struct XCtorInit {
184    XCtorInit(std::initializer_list<X<int>>);
185  };
186
187  void f(X<int> &xi) {
188    XCtorInit xc = { xi, xi };
189  }
190}
191
192namespace PR14272 {
193  auto x { { 0, 0 } }; // expected-error {{cannot deduce actual type for variable 'x' with type 'auto' from initializer list}}
194}
195
196namespace initlist_of_array {
197  void f(std::initializer_list<int[2]>) {}
198  void f(std::initializer_list<int[2][2]>) = delete;
199  void h() {
200    f({{1,2},{3,4}});
201  }
202}
203
204namespace init_list_deduction_failure {
205  void f();
206  void f(int);
207  template<typename T> void g(std::initializer_list<T>);
208  // expected-note@-1 {{candidate template ignored: couldn't resolve reference to overloaded function 'f'}}
209  void h() { g({f}); }
210  // expected-error@-1 {{no matching function for call to 'g'}}
211}
212
213namespace deleted_copy {
214  struct X {
215    X(int i) {}
216    X(const X& x) = delete; // expected-note {{here}}
217    void operator=(const X& x) = delete;
218  };
219
220  std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}}
221}
222
223namespace RefVersusInitList {
224  struct S {};
225  void f(const S &) = delete;
226  void f(std::initializer_list<S>);
227  void g(S s) { f({S()}); }
228}
229
230namespace PR18013 {
231  int f();
232  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')}}
233}
234
235namespace DR1070 {
236  struct S {
237    S(std::initializer_list<int>);
238  };
239  S s[3] = { {1, 2, 3}, {4, 5} }; // ok
240  S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok
241}
242
243namespace ListInitInstantiate {
244  struct A {
245    A(std::initializer_list<A>);
246    A(std::initializer_list<int>);
247  };
248  struct B : A {
249    B(int);
250  };
251  template<typename T> struct X {
252    X();
253    A a;
254  };
255  template<typename T> X<T>::X() : a{B{0}, B{1}} {}
256
257  X<int> x;
258
259  int f(const A&);
260  template<typename T> void g() { int k = f({0}); }
261  template void g<int>();
262}
263
264namespace TemporaryInitListSourceRange_PR22367 {
265  struct A {
266    constexpr A() {}
267    A(std::initializer_list<int>); // expected-note {{here}}
268  };
269  constexpr int f(A) { return 0; }
270  constexpr int k = f( // expected-error {{must be initialized by a constant expression}}
271      // The point of this test is to check that the caret points to
272      // 'std::initializer_list', not to '{0}'.
273      std::initializer_list // expected-note {{constructor}}
274      <int>
275      {0}
276      );
277}
278