1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
3 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -DUNION_TEST -verify %s
8 union initializer_list
{
9 // expected-error@-1 {{'std::initializer_list<int>' layout not recognized.}}
11 decltype(sizeof 0) size
;
17 // This must obviously come before the definition of std::initializer_list.
18 void missing_initializerlist() {
19 auto l
= {1, 2, 3, 4}; // expected-error {{std::initializer_list was not found}}
23 typedef decltype(sizeof(int)) size_t;
25 // libc++'s implementation
27 class initializer_list
32 initializer_list(const _E
* __b
, size_t __s
)
38 typedef _E value_type
;
39 typedef const _E
& reference
;
40 typedef const _E
& const_reference
;
41 typedef size_t size_type
;
43 typedef const _E
* iterator
;
44 typedef const _E
* const_iterator
;
46 constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
48 constexpr size_t size() const {return __size_
;}
49 const _E
* begin() const {return __begin_
;}
50 const _E
* end() const {return __begin_
+ __size_
;}
54 template <typename T
, typename U
>
55 struct same_type
{ static const bool value
= false; };
57 struct same_type
<T
, T
> { static const bool value
= true; };
59 struct one
{ char c
[1]; };
60 struct two
{ char c
[2]; };
72 std::initializer_list
<int> il
= { 1, 2, 3 };
73 std::initializer_list
<double> dl
= { 1.0, 2.0, 3 };
74 std::initializer_list
<A
> al
= { {1, 2}, {2, 3}, {3, 4} };
75 std::initializer_list
<B
> bl
= { {1, 2}, {2, 3}, {} };
78 void function_call() {
79 void f(std::initializer_list
<int>);
82 void g(std::initializer_list
<B
>);
83 g({ {1, 2}, {2, 3}, {} });
96 void overloaded_call() {
97 one
overloaded(std::initializer_list
<int>);
98 two
overloaded(std::initializer_list
<B
>);
100 static_assert(sizeof(overloaded({1, 2, 3})) == sizeof(one
), "bad overload");
101 static_assert(sizeof(overloaded({ {1, 2}, {2, 3}, {} })) == sizeof(two
), "bad overload");
103 void ambiguous(std::initializer_list
<A
>); // expected-note {{candidate}}
104 void ambiguous(std::initializer_list
<B
>); // expected-note {{candidate}}
105 ambiguous({ {1, 2}, {2, 3}, {3, 4} }); // expected-error {{ambiguous}}
107 one
ov2(std::initializer_list
<int>); // expected-note {{candidate}}
108 two
ov2(std::initializer_list
<C
>); // expected-note {{candidate}}
109 // Worst sequence to int is identity, whereas to C it's user-defined.
110 static_assert(sizeof(ov2({1, 2, 3})) == sizeof(one
), "bad overload");
111 // But here, user-defined is worst in both cases.
112 ov2({1, 2, D()}); // expected-error {{ambiguous}}
115 template <typename T
>
116 T
deduce(std::initializer_list
<T
>); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
117 template <typename T
>
118 T
deduce_ref(const std::initializer_list
<T
>&); // expected-note {{conflicting types for parameter 'T' ('int' vs. 'double')}}
120 template<typename T
, typename U
> struct pair
{ pair(...); };
121 template<typename T
> void deduce_pairs(std::initializer_list
<pair
<T
, typename
T::type
>>);
122 // 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]}}
123 struct WithIntType
{ typedef int type
; };
125 template<typename
...T
> void deduce_after_init_list_in_pack(void (*)(T
...), T
...); // expected-note {{<int, int> vs. <(no value), double>}}
127 void argument_deduction() {
128 static_assert(same_type
<decltype(deduce({1, 2, 3})), int>::value
, "bad deduction");
129 static_assert(same_type
<decltype(deduce({1.0, 2.0, 3.0})), double>::value
, "bad deduction");
131 deduce({1, 2.0}); // expected-error {{no matching function}}
133 static_assert(same_type
<decltype(deduce_ref({1, 2, 3})), int>::value
, "bad deduction");
134 static_assert(same_type
<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value
, "bad deduction");
136 deduce_ref({1, 2.0}); // expected-error {{no matching function}}
138 pair
<WithIntType
, int> pi
;
139 pair
<WithIntType
, float> pf
;
140 deduce_pairs({pi
, pi
, pi
}); // ok
141 deduce_pairs({pi
, pf
, pi
}); // expected-error {{no matching function}}
143 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0);
144 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0.0); // expected-error {{no matching function}}
147 void auto_deduction() {
148 auto l
= {1, 2, 3, 4};
149 auto l2
{1, 2, 3, 4}; // expected-error {{initializer for variable 'l2' with type 'auto' contains multiple expressions}}
151 static_assert(same_type
<decltype(l
), std::initializer_list
<int>>::value
, "");
152 static_assert(same_type
<decltype(l3
), int>::value
, "");
153 auto bl
= {1, 2.0}; // expected-error {{deduced conflicting types ('int' vs 'double') for initializer list element type}}
155 void f1(int), f1(float), f2(int), f3(float);
158 auto fl
= {f1
, f2
, f3
}; // expected-error {{deduced conflicting types ('void (*)(int)' vs 'void (*)(float)') for initializer list element type}}
160 for (int i
: {1, 2, 3, 4}) {}
161 for (int j
: {1.0, 2.0, 3.0f
, 4.0}) {} // expected-error {{deduced conflicting types ('double' vs 'float') for initializer list element type}}
165 new auto{1, 2, 3}; // expected-error {{new expression for type 'auto' contains multiple constructor arguments}}
166 new std::initializer_list
<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}}
170 std::initializer_list
<int> il
// expected-note {{declared here}}
171 = {1, 2, 3}; // ok, unused
172 std::initializer_list
<int> jl
{1, 2, 3}; // expected-note {{default member init}}
176 haslist1::haslist1() // expected-error {{backing array for 'std::initializer_list' member 'jl' is a temporary object}}
177 : il
{1, 2, 3} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
181 // Deduction with nested initializer lists.
182 template<typename T
> void f(std::initializer_list
<T
>);
183 template<typename T
> void g(std::initializer_list
<std::initializer_list
<T
>>);
186 f({0, {1}}); // expected-warning{{braces around scalar initializer}}
188 std::initializer_list
<int> il
= {1, 2};
195 void f(std::initializer_list
<T
>) {
196 T x
= 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}}
200 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}}
202 auto x
= { "A", "BB", "CCC" };
203 std::initializer_list
<const char *> *il
= &x
;
205 for( auto s
: {"A", "BB", "CCC", "DDD"}) { }
212 X(std::initializer_list
<int>, T
);
218 namespace rdar11948732
{
219 template<typename T
> struct X
{};
222 XCtorInit(std::initializer_list
<X
<int>>);
226 XCtorInit xc
= { xi
, xi
};
231 auto x
{ { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}}
234 namespace initlist_of_array
{
235 void f(std::initializer_list
<int[2]>) {}
236 void f(std::initializer_list
<int[2][2]>) = delete;
242 namespace init_list_deduction_failure
{
245 // FIXME: It'd be nice to track that 'T' became a non-deduced context due to
246 // overload resolution failure for 'f'.
247 template<typename T
> void g(std::initializer_list
<T
>);
248 // expected-note@-1 {{candidate template ignored: couldn't infer template argument 'T'}}
250 g({f
}); // expected-error {{no matching function for call to 'g'}}
255 namespace deleted_copy
{
258 X(const X
& x
) = delete; // expected-note {{here}}
259 void operator=(const X
& x
) = delete;
262 std::initializer_list
<X
> x
{1}; // expected-error {{invokes deleted constructor}}
265 namespace RefVersusInitList
{
267 void f(const S
&) = delete;
268 void f(std::initializer_list
<S
>);
269 void g(S s
) { f({S()}); }
274 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')}}
279 S(std::initializer_list
<int>);
281 S s
[3] = { {1, 2, 3}, {4, 5} }; // ok
282 S
*p
= new S
[3] { {1, 2, 3}, {4, 5} }; // ok
285 namespace ListInitInstantiate
{
287 A(std::initializer_list
<A
>);
288 A(std::initializer_list
<int>);
293 template<typename T
> struct X
{
297 template<typename T
> X
<T
>::X() : a
{B
{0}, B
{1}} {}
302 template<typename T
> void g() { int k
= f({0}); }
303 template void g
<int>();
306 namespace TemporaryInitListSourceRange_PR22367
{
309 A(std::initializer_list
<int>); // expected-note {{here}}
311 constexpr int f(A
) { return 0; }
312 constexpr int k
= f( // expected-error {{must be initialized by a constant expression}}
313 // The point of this test is to check that the caret points to
314 // 'std::initializer_list', not to '{0}'.
315 std::initializer_list
// expected-note {{constructor}}
321 namespace ParameterPackNestedInitializerLists_PR23904c3
{
322 template <typename
...T
>
323 void f(std::initializer_list
<std::initializer_list
<T
>> ...tt
); // expected-note 2{{conflicting}} expected-note {{incomplete pack}}
326 f({{0}}, {{'\0'}}); // ok, T = <int, char>
327 f({{0}, {'\0'}}); // expected-error {{no match}}
328 f({{0, '\0'}}); // expected-error {{no match}}
330 f({{0}}, {{{}}}); // expected-error {{no match}}
331 f({{0}}, {{{}, '\0'}}); // ok, T = <int, char>
332 f({{0}, {{}}}); // ok, T = <int>
333 f({{0, {}}}); // ok, T = <int>
337 namespace update_rbrace_loc_crash
{
338 // We used to crash-on-invalid on this example when updating the right brace
340 template <typename T
, T
>
342 template <typename T
, typename F
, int... I
>
343 std::initializer_list
<T
> ExplodeImpl(F p1
, A
<int, I
...>) {
344 // expected-error@+1 {{reference to incomplete type 'const Incomplete' could not bind to an rvalue of type 'void'}}
347 template <typename T
, int N
, typename F
>
349 // expected-note@+1 {{in instantiation of function template specialization}}
350 ExplodeImpl
<T
>(p1
, A
<int, N
>());
353 struct ContainsIncomplete
{
354 const Incomplete
&obstacle
;
357 // expected-note@+1 {{in instantiation of function template specialization}}
358 Explode
<ContainsIncomplete
, 4>([](int) {});
362 namespace no_conversion_after_auto_list_deduction
{
363 // We used to deduce 'auto' == 'std::initializer_list<X>' here, and then
364 // incorrectly accept the declaration of 'x'.
365 struct X
{ using T
= std::initializer_list
<X
> X::*; operator T(); };
366 auto X::*x
= { X() }; // expected-error {{from initializer list}}
368 struct Y
{ using T
= std::initializer_list
<Y
>(*)(); operator T(); };
369 auto (*y
)() = { Y() }; // expected-error {{from initializer list}}
372 namespace designated_init
{
373 constexpr auto a
= {.a
= 1, .b
= 2}; // expected-error {{cannot deduce}}
374 constexpr auto b
= {[0] = 1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning {{C99}}
375 constexpr auto c
= {1, [4] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
376 constexpr auto d
= {1, [0] = 2}; // expected-error {{cannot deduce}} expected-warning 2{{C99}} expected-note {{here}}
378 // If we ever start accepting the above, these assertions should pass.
379 static_assert(c
.size() == 5, "");
380 static_assert(d
.size() == 1, "");
383 namespace weird_initlist
{
387 template<> struct std::initializer_list
<weird_initlist::weird
<0>> { int a
, b
, c
; };
388 // expected-error@-1 2 {{'std::initializer_list<weird<0>>' layout not recognized}}
389 template<> struct std::initializer_list
<weird_initlist::weird
<1>> { std::size_t sz
; const weird_initlist::weird
<1>* p
; };
390 // expected-error@-1 {{'std::initializer_list<weird<1>>' layout not recognized}}
391 template<> struct std::initializer_list
<weird_initlist::weird
<2>> { const weird_initlist::weird
<2>* first
; const weird_initlist::weird
<2>* last
; };
392 template<> struct std::initializer_list
<weird_initlist::weird
<3>> { weird_initlist::weird
<3>* p
; std::size_t sz
; };
393 // expected-error@-1 {{'std::initializer_list<weird<3>>' layout not recognized}}
394 template<> struct std::initializer_list
<weird_initlist::weird
<4>> { const weird_initlist::weird
<4>& p
; std::size_t sz
; };
395 // expected-error@-1 {{'std::initializer_list<weird<4>>' layout not recognized}}
396 template<> struct std::initializer_list
<weird_initlist::weird
<5>> { const weird_initlist::weird
<5>* p
; std::size_t : sizeof(std::size_t) * __CHAR_BIT__
; };
397 // expected-error@-1 {{'std::initializer_list<weird<5>>' layout not recognized}}
398 template<> struct std::initializer_list
<weird_initlist::weird
<6>> { const weird_initlist::weird
<6>* p
; std::size_t sz
: sizeof(std::size_t) * __CHAR_BIT__
; };
399 // expected-error@-1 {{'std::initializer_list<weird<6>>' layout not recognized}}
400 struct empty_base
{};
401 template<> struct std::initializer_list
<weird_initlist::weird
<7>> : empty_base
{ const weird_initlist::weird
<7>* p
; std::size_t sz
; };
402 // expected-error@-1 {{'std::initializer_list<weird<7>>' layout not recognized}}
403 template<> struct std::initializer_list
<weird_initlist::weird
<8>> { const weird_initlist::weird
<8>* p
; std::size_t sz
; ~initializer_list(); };
404 template<> struct std::initializer_list
<weird_initlist::weird
<9>> { const weird_initlist::weird
<9>* p
; std::size_t sz
; virtual void f(); };
405 // expected-error@-1 {{'std::initializer_list<weird<9>>' layout not recognized}}
406 template<> struct std::initializer_list
<weird_initlist::weird
<10>> { const weird_initlist::weird
<10>* p
; alignas(64) std::size_t sz
; };
407 template<> struct std::initializer_list
<weird_initlist::weird
<11>>;
408 // expected-note@-1 {{forward declaration of 'std::initializer_list<weird_initlist::weird<11>>'}}
409 namespace weird_initlist
{
410 auto _0
= {weird
<0>{}, weird
<0>{}, weird
<0>{}, weird
<0>{}, weird
<0>{}};
411 constexpr auto _0c
= {weird
<0>{}, weird
<0>{}, weird
<0>{}, weird
<0>{}, weird
<0>{}};
412 auto _1
= {weird
<1>{}, weird
<1>{}};
413 auto _2
= {weird
<2>{}, weird
<2>{}, weird
<2>{}};
414 constexpr auto _2c
= {weird
<2>{}, weird
<2>{}, weird
<2>{}}; // (Two pointer representation is supported)
415 static_assert(_2c
.first
+ 3 == _2c
.last
, "");
416 auto _3
= {weird
<3>{}, weird
<3>{}};
417 auto _4
= {weird
<4>{}, weird
<4>{}};
418 auto _5
= {weird
<5>{}, weird
<5>{}};
419 auto _6
= {weird
<6>{}, weird
<6>{}};
420 auto _7
= {weird
<7>{}, weird
<7>{}};
421 auto _8
= {weird
<8>{}, weird
<8>{}};
422 auto _9
= {weird
<9>{}, weird
<9>{}};
423 auto _10
= {weird
<10>{}, weird
<10>{}};
424 constexpr auto _10c
= {weird
<10>{}, weird
<10>{}, weird
<10>{}};
425 static_assert(_10c
.sz
== 3, "");
426 const auto& _11
= {weird
<11>{}, weird
<11>{}}; // expected-error {{initialization of incomplete type 'const std::initializer_list<weird<11>>'}}
429 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}}
431 std::initializer_list
<int> get(int cond
) {
435 return {1, 2, 3}; // expected-warning {{returning address of local temporary object}}
436 return std::initializer_list
<int>{1, 2, 3}; // expected-warning {{returning address of local temporary object}}