1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
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}}
9 typedef decltype(sizeof(int)) size_t;
11 // libc++'s implementation
13 class initializer_list
18 initializer_list(const _E
* __b
, size_t __s
)
24 typedef _E value_type
;
25 typedef const _E
& reference
;
26 typedef const _E
& const_reference
;
27 typedef size_t size_type
;
29 typedef const _E
* iterator
;
30 typedef const _E
* const_iterator
;
32 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
34 constexpr size_t size() const {return __size_
;}
35 const _E
* begin() const {return __begin_
;}
36 const _E
* end() const {return __begin_
+ __size_
;}
40 template <typename T
, typename U
>
41 struct same_type
{ static const bool value
= false; };
43 struct same_type
<T
, T
> { static const bool value
= true; };
45 struct one
{ char c
[1]; };
46 struct two
{ char c
[2]; };
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}, {} };
64 void function_call() {
65 void f(std::initializer_list
<int>);
68 void g(std::initializer_list
<B
>);
69 g({ {1, 2}, {2, 3}, {} });
82 void overloaded_call() {
83 one
overloaded(std::initializer_list
<int>);
84 two
overloaded(std::initializer_list
<B
>);
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");
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}}
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}}
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')}}
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
; };
111 template<typename
...T
> void deduce_after_init_list_in_pack(void (*)(T
...), T
...); // expected-note {{<int, int> vs. <(no value), double>}}
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");
117 deduce({1, 2.0}); // expected-error {{no matching function}}
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");
122 deduce_ref({1, 2.0}); // expected-error {{no matching function}}
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}}
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}}
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}}
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}}
141 void f1(int), f1(float), f2(int), f3(float);
144 auto fl
= {f1
, f2
, f3
}; // expected-error {{deduced conflicting types ('void (*)(int)' vs 'void (*)(float)') for initializer list element type}}
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}}
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}}
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}}
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}}
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
>>);
172 f({0, {1}}); // expected-warning{{braces around scalar initializer}}
174 std::initializer_list
<int> il
= {1, 2};
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'}}
186 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
188 auto x
= { "A", "BB", "CCC" };
189 std::initializer_list
<const char *> *il
= &x
;
191 for( auto s
: {"A", "BB", "CCC", "DDD"}) { }
198 X(std::initializer_list
<int>, T
);
204 namespace rdar11948732
{
205 template<typename T
> struct X
{};
208 XCtorInit(std::initializer_list
<X
<int>>);
212 XCtorInit xc
= { xi
, xi
};
217 auto x
{ { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
220 namespace initlist_of_array
{
221 void f(std::initializer_list
<int[2]>) {}
222 void f(std::initializer_list
<int[2][2]>) = delete;
228 namespace init_list_deduction_failure
{
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'}}
236 g({f
}); // expected-error {{no matching function for call to 'g'}}
241 namespace deleted_copy
{
244 X(const X
& x
) = delete; // expected-note {{here}}
245 void operator=(const X
& x
) = delete;
248 std::initializer_list
<X
> x
{1}; // expected-error {{invokes deleted constructor}}
251 namespace RefVersusInitList
{
253 void f(const S
&) = delete;
254 void f(std::initializer_list
<S
>);
255 void g(S s
) { f({S()}); }
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')}}
265 S(std::initializer_list
<int>);
267 S s
[3] = { {1, 2, 3}, {4, 5} }; // ok
268 S
*p
= new S
[3] { {1, 2, 3}, {4, 5} }; // ok
271 namespace ListInitInstantiate
{
273 A(std::initializer_list
<A
>);
274 A(std::initializer_list
<int>);
279 template<typename T
> struct X
{
283 template<typename T
> X
<T
>::X() : a
{B
{0}, B
{1}} {}
288 template<typename T
> void g() { int k
= f({0}); }
289 template void g
<int>();
292 namespace TemporaryInitListSourceRange_PR22367
{
295 A(std::initializer_list
<int>); // expected-note {{here}}
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}}
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}}
312 f({{0}}, {{'\0'}}); // ok, T = <int, char>
313 f({{0}, {'\0'}}); // expected-error {{no match}}
314 f({{0, '\0'}}); // expected-error {{no match}}
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>
323 namespace update_rbrace_loc_crash
{
324 // We used to crash-on-invalid on this example when updating the right brace
326 template <typename T
, T
>
328 template <typename T
, typename F
, int... I
>
329 std::initializer_list
<T
> ExplodeImpl(F p1
, A
<int, I
...>) {
330 // expected-error@+1 {{reference to incomplete type 'const update_rbrace_loc_crash::Incomplete' could not bind to an rvalue of type 'void'}}
333 template <typename T
, int N
, typename F
>
335 // expected-note@+1 {{in instantiation of function template specialization}}
336 ExplodeImpl
<T
>(p1
, A
<int, N
>());
339 struct ContainsIncomplete
{
340 const Incomplete
&obstacle
;
343 // expected-note@+1 {{in instantiation of function template specialization}}
344 Explode
<ContainsIncomplete
, 4>([](int) {});
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}}
354 struct Y
{ using T
= std::initializer_list
<Y
>(*)(); operator T(); };
355 auto (*y
)() = { Y() }; // expected-error {{from initializer list}}
358 namespace designated_init
{
359 constexpr auto a
= {.a
= 1, .b
= 2}; // expected-error {{cannot deduce}}
360 constexpr auto b
= {[0] = 1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning {{C99}}
361 constexpr auto c
= {1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
362 constexpr auto d
= {1, [0] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
364 // If we ever start accepting the above, these assertions should pass.
365 static_assert(c
.size() == 5, "");
366 static_assert(d
.size() == 1, "");
369 namespace weird_initlist
{
372 template<> struct std::initializer_list
<weird_initlist::weird
> { int a
, b
, c
; };
373 namespace weird_initlist
{
374 // We don't check the struct layout in Sema.
375 auto x
= {weird
{}, weird
{}, weird
{}, weird
{}, weird
{}};
376 // ... but we do in constant evaluation.
377 constexpr auto y
= {weird
{}, weird
{}, weird
{}, weird
{}, weird
{}}; // expected-error {{constant}} expected-note {{type 'const std::initializer_list<weird_initlist::weird>' has unexpected layout}}
380 auto v
= std::initializer_list
<int>{1,2,3}; // expected-warning {{array backing local initializer list 'v' will be destroyed at the end of the full-expression}}
382 std::initializer_list
<int> get(int cond
) {
386 return {1, 2, 3}; // expected-warning {{returning address of local temporary object}}
387 return std::initializer_list
<int>{1, 2, 3}; // expected-warning {{returning address of local temporary object}}