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>>); // expected-note {{something}} 108 struct WithIntType { typedef int type; }; 109 110 template<typename ...T> void deduce_after_init_list_in_pack(void (*)(T...), T...); // expected-note {{<int, int> vs. <(no value), double>}} 111 112 void argument_deduction() { 113 static_assert(same_type<decltype(deduce({1, 2, 3})), int>::value, "bad deduction"); 114 static_assert(same_type<decltype(deduce({1.0, 2.0, 3.0})), double>::value, "bad deduction"); 115 116 deduce({1, 2.0}); // expected-error {{no matching function}} 117 118 static_assert(same_type<decltype(deduce_ref({1, 2, 3})), int>::value, "bad deduction"); 119 static_assert(same_type<decltype(deduce_ref({1.0, 2.0, 3.0})), double>::value, "bad deduction"); 120 121 deduce_ref({1, 2.0}); // expected-error {{no matching function}} 122 123 pair<WithIntType, int> pi; 124 pair<WithIntType, float> pf; 125 deduce_pairs({pi, pi, pi}); // ok 126 deduce_pairs({pi, pf, pi}); // expected-error {{no matching function}} 127 128 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0); 129 deduce_after_init_list_in_pack((void(*)(int,int))0, {}, 0.0); // expected-error {{no matching function}} 130 } 131 132 void auto_deduction() { 133 auto l = {1, 2, 3, 4}; 134 auto l2 {1, 2, 3, 4}; // expected-error {{initializer for variable 'l2' with type 'auto' contains multiple expressions}} 135 auto l3 {1}; 136 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, ""); 137 static_assert(same_type<decltype(l3), int>::value, ""); 138 auto bl = {1, 2.0}; // expected-error {{cannot deduce}} 139 140 for (int i : {1, 2, 3, 4}) {} 141 } 142 143 void dangle() { 144 new auto{1, 2, 3}; // expected-error {{cannot use list-initialization}} 145 new std::initializer_list<int>{1, 2, 3}; // expected-warning {{at the end of the full-expression}} 146 } 147 148 struct haslist1 { 149 std::initializer_list<int> il = {1, 2, 3}; // expected-warning{{at the end of the constructor}} 150 std::initializer_list<int> jl{1, 2, 3}; // expected-warning{{at the end of the constructor}} 151 haslist1(); 152 }; 153 154 haslist1::haslist1() 155 : il{1, 2, 3} // expected-warning{{at the end of the constructor}} 156 {} 157 158 namespace PR12119 { 159 // Deduction with nested initializer lists. 160 template<typename T> void f(std::initializer_list<T>); 161 template<typename T> void g(std::initializer_list<std::initializer_list<T>>); 162 163 void foo() { 164 f({0, {1}}); // expected-warning{{braces around scalar initializer}} 165 g({{0, 1}, {2, 3}}); 166 std::initializer_list<int> il = {1, 2}; 167 g({il, {2, 3}}); 168 } 169 } 170 171 namespace Decay { 172 template<typename T> 173 void f(std::initializer_list<T>) { 174 T x = 1; // expected-error{{cannot initialize a variable of type 'const char *' with an rvalue of type 'int'}} 175 } 176 177 void g() { 178 f({"A", "BB", "CCC"}); // expected-note{{in instantiation of function template specialization 'Decay::f<const char *>' requested here}} 179 180 auto x = { "A", "BB", "CCC" }; 181 std::initializer_list<const char *> *il = &x; 182 183 for( auto s : {"A", "BB", "CCC", "DDD"}) { } 184 } 185 } 186 187 namespace PR12436 { 188 struct X { 189 template<typename T> 190 X(std::initializer_list<int>, T); 191 }; 192 193 X x({}, 17); 194 } 195 196 namespace rdar11948732 { 197 template<typename T> struct X {}; 198 199 struct XCtorInit { 200 XCtorInit(std::initializer_list<X<int>>); 201 }; 202 203 void f(X<int> &xi) { 204 XCtorInit xc = { xi, xi }; 205 } 206 } 207 208 namespace PR14272 { 209 auto x { { 0, 0 } }; // expected-error {{cannot deduce type for variable 'x' with type 'auto' from nested initializer list}} 210 } 211 212 namespace initlist_of_array { 213 void f(std::initializer_list<int[2]>) {} 214 void f(std::initializer_list<int[2][2]>) = delete; 215 void h() { 216 f({{1,2},{3,4}}); 217 } 218 } 219 220 namespace init_list_deduction_failure { 221 void f(); 222 void f(int); 223 template<typename T> void g(std::initializer_list<T>); 224 // expected-note@-1 {{candidate template ignored: couldn't resolve reference to overloaded function 'f'}} 225 void h() { g({f}); } 226 // expected-error@-1 {{no matching function for call to 'g'}} 227 } 228 229 namespace deleted_copy { 230 struct X { 231 X(int i) {} 232 X(const X& x) = delete; // expected-note {{here}} 233 void operator=(const X& x) = delete; 234 }; 235 236 std::initializer_list<X> x{1}; // expected-error {{invokes deleted constructor}} 237 } 238 239 namespace RefVersusInitList { 240 struct S {}; 241 void f(const S &) = delete; 242 void f(std::initializer_list<S>); 243 void g(S s) { f({S()}); } 244 } 245 246 namespace PR18013 { 247 int f(); 248 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')}} 249 } 250 251 namespace DR1070 { 252 struct S { 253 S(std::initializer_list<int>); 254 }; 255 S s[3] = { {1, 2, 3}, {4, 5} }; // ok 256 S *p = new S[3] { {1, 2, 3}, {4, 5} }; // ok 257 } 258 259 namespace ListInitInstantiate { 260 struct A { 261 A(std::initializer_list<A>); 262 A(std::initializer_list<int>); 263 }; 264 struct B : A { 265 B(int); 266 }; 267 template<typename T> struct X { 268 X(); 269 A a; 270 }; 271 template<typename T> X<T>::X() : a{B{0}, B{1}} {} 272 273 X<int> x; 274 275 int f(const A&); 276 template<typename T> void g() { int k = f({0}); } 277 template void g<int>(); 278 } 279 280 namespace TemporaryInitListSourceRange_PR22367 { 281 struct A { 282 constexpr A() {} 283 A(std::initializer_list<int>); // expected-note {{here}} 284 }; 285 constexpr int f(A) { return 0; } 286 constexpr int k = f( // expected-error {{must be initialized by a constant expression}} 287 // The point of this test is to check that the caret points to 288 // 'std::initializer_list', not to '{0}'. 289 std::initializer_list // expected-note {{constructor}} 290 <int> 291 {0} 292 ); 293 } 294 295 namespace ParameterPackNestedInitializerLists_PR23904c3 { 296 template <typename ...T> 297 void f(std::initializer_list<std::initializer_list<T>> ...tt); 298 299 void foo() { f({{0}}, {{'\0'}}); } 300 } 301 302 namespace update_rbrace_loc_crash { 303 // We used to crash-on-invalid on this example when updating the right brace 304 // location. 305 template <typename T, T> 306 struct A {}; 307 template <typename T, typename F, int... I> 308 std::initializer_list<T> ExplodeImpl(F p1, A<int, I...>) { 309 // expected-error@+1 {{reference to type 'const update_rbrace_loc_crash::Incomplete' could not bind to an rvalue of type 'void'}} 310 return {p1(I)...}; 311 } 312 template <typename T, int N, typename F> 313 void Explode(F p1) { 314 // expected-note@+1 {{in instantiation of function template specialization}} 315 ExplodeImpl<T>(p1, A<int, N>()); 316 } 317 class Incomplete; 318 struct ContainsIncomplete { 319 const Incomplete &obstacle; 320 }; 321 void f() { 322 // expected-note@+1 {{in instantiation of function template specialization}} 323 Explode<ContainsIncomplete, 4>([](int) {}); 324 } 325 } 326