blob: 88571d671b0737a8afacf17951a82398aa1c48fa [file] [log] [blame]
Sebastian Redl2b916b82012-01-17 22:49:42 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2
Sebastian Redlb832f6d2012-01-23 22:09:39 +00003// 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
Sebastian Redl2b916b82012-01-17 22:49:42 +00008namespace 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
Sebastian Redl84760e32012-01-17 22:49:58 +000040template <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
Sebastian Redl2b916b82012-01-17 22:49:42 +000045struct 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}
Sebastian Redlfe592282012-01-17 22:49:48 +000071
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}
Sebastian Redl84760e32012-01-17 22:49:58 +0000100
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}
Sebastian Redl62b7cfb2012-01-17 22:50:08 +0000117
118void auto_deduction() {
119 auto l = {1, 2, 3, 4};
120 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
121 auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
122
123 for (int i : {1, 2, 3, 4}) {}
124}
Sebastian Redl6dc00f62012-02-12 18:41:05 +0000125
126void dangle() {
127 new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}}
128 new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
129}
Sebastian Redl772291a2012-02-19 16:31:05 +0000130
131struct haslist1 {
132 std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}}
Sebastian Redl33deb352012-02-22 10:50:08 +0000133 std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}}
Sebastian Redl772291a2012-02-19 16:31:05 +0000134 haslist1();
135};
136
137haslist1::haslist1()
138: il{1, 2, 3} // expected-warning{{at the end of the constructor}}
139{}
Sebastian Redl4b911e62012-03-15 21:40:51 +0000140
141namespace PR12119 {
142 // Deduction with nested initializer lists.
143 template<typename T> void f(std::initializer_list<T>);
144 template<typename T> void g(std::initializer_list<std::initializer_list<T>>);
145
146 void foo() {
147 f({0, {1}});
148 g({{0, 1}, {2, 3}});
149 std::initializer_list<int> il = {1, 2};
150 g({il, {2, 3}});
151 }
152}
Douglas Gregord2803892012-04-04 05:10:53 +0000153
154namespace Decay {
155 template<typename T>
156 void f(std::initializer_list<T>) {
157 T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
158 }
159
160 void g() {
161 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
162
163 auto x = { "A", "BB", "CCC" };
164 std::initializer_list<const char *> *il = &x;
165
166 for( auto s : {"A", "BB", "CCC", "DDD"}) { }
167 }
168}
Douglas Gregor5b4bf132012-04-04 23:09:20 +0000169
170namespace PR12436 {
171 struct X {
172 template<typename T>
173 X(std::initializer_list<int>, T);
174 };
175
176 X x({}, 17);
177}
Douglas Gregor3c394c52012-07-31 22:15:04 +0000178
179namespace rdar11948732 {
180 template<typename T> struct X {};
181
182 struct XCtorInit {
183 XCtorInit(std::initializer_list<X<int>>);
184 };
185
186 void f(X<int> &xi) {
187 XCtorInit xc = { xi, xi };
188 }
189}
Eli Friedman70a01892012-11-06 23:56:42 +0000190
191namespace PR14272 {
192 auto x { { 0, 0 } }; // expected-error {{cannot deduce actual type for variable 'x' with type 'auto' from initializer list}}
193}
Richard Smith2801d9a2012-12-09 06:48:56 +0000194
195namespace initlist_of_array {
196 void f(std::initializer_list<int[2]>) {}
197 void f(std::initializer_list<int[2][2]>) = delete;
198 void h() {
199 f({{1,2},{3,4}});
200 }
201}
Richard Smith0efa62f2013-01-31 04:03:12 +0000202
203namespace init_list_deduction_failure {
204 void f();
205 void f(int);
206 template<typename T> void g(std::initializer_list<T>);
207 // expected-note@-1 {{candidate template ignored: couldn't resolve reference to overloaded function 'f'}}
208 void h() { g({f}); }
209 // expected-error@-1 {{no matching function for call to 'g'}}
210}