1*67e74705SXin Li // RUN: %clang_cc1 -std=c++0x -fsyntax-only -fexceptions -verify %s 2*67e74705SXin Li 3*67e74705SXin Li struct one { char c[1]; }; 4*67e74705SXin Li struct two { char c[2]; }; 5*67e74705SXin Li 6*67e74705SXin Li namespace std { 7*67e74705SXin Li typedef decltype(sizeof(int)) size_t; 8*67e74705SXin Li 9*67e74705SXin Li // libc++'s implementation 10*67e74705SXin Li template <class _E> 11*67e74705SXin Li class initializer_list 12*67e74705SXin Li { 13*67e74705SXin Li const _E* __begin_; 14*67e74705SXin Li size_t __size_; 15*67e74705SXin Li initializer_list(const _E * __b,size_t __s)16*67e74705SXin Li initializer_list(const _E* __b, size_t __s) 17*67e74705SXin Li : __begin_(__b), 18*67e74705SXin Li __size_(__s) 19*67e74705SXin Li {} 20*67e74705SXin Li 21*67e74705SXin Li public: 22*67e74705SXin Li typedef _E value_type; 23*67e74705SXin Li typedef const _E& reference; 24*67e74705SXin Li typedef const _E& const_reference; 25*67e74705SXin Li typedef size_t size_type; 26*67e74705SXin Li 27*67e74705SXin Li typedef const _E* iterator; 28*67e74705SXin Li typedef const _E* const_iterator; 29*67e74705SXin Li initializer_list()30*67e74705SXin Li initializer_list() : __begin_(nullptr), __size_(0) {} 31*67e74705SXin Li size() const32*67e74705SXin Li size_t size() const {return __size_;} begin() const33*67e74705SXin Li const _E* begin() const {return __begin_;} end() const34*67e74705SXin Li const _E* end() const {return __begin_ + __size_;} 35*67e74705SXin Li }; 36*67e74705SXin Li } 37*67e74705SXin Li 38*67e74705SXin Li namespace objects { 39*67e74705SXin Li 40*67e74705SXin Li struct X1 { X1(int); }; 41*67e74705SXin Li struct X2 { explicit X2(int); }; // expected-note {{constructor declared here}} 42*67e74705SXin Li 43*67e74705SXin Li template <int N> 44*67e74705SXin Li struct A { Aobjects::A45*67e74705SXin Li A() { static_assert(N == 0, ""); } Aobjects::A46*67e74705SXin Li A(int, double) { static_assert(N == 1, ""); } 47*67e74705SXin Li }; 48*67e74705SXin Li 49*67e74705SXin Li template <int N> 50*67e74705SXin Li struct F { Fobjects::F51*67e74705SXin Li F() { static_assert(N == 0, ""); } Fobjects::F52*67e74705SXin Li F(int, double) { static_assert(N == 1, ""); } Fobjects::F53*67e74705SXin Li F(std::initializer_list<int>) { static_assert(N == 3, ""); } 54*67e74705SXin Li }; 55*67e74705SXin Li 56*67e74705SXin Li template <int N> 57*67e74705SXin Li struct D { Dobjects::D58*67e74705SXin Li D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}} Dobjects::D59*67e74705SXin Li D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}} 60*67e74705SXin Li }; 61*67e74705SXin Li 62*67e74705SXin Li template <int N> 63*67e74705SXin Li struct E { Eobjects::E64*67e74705SXin Li E(int, int) { static_assert(N == 0, ""); } Eobjects::E65*67e74705SXin Li E(X1, int) { static_assert(N == 1, ""); } 66*67e74705SXin Li }; 67*67e74705SXin Li overload_resolution()68*67e74705SXin Li void overload_resolution() { 69*67e74705SXin Li { A<0> a{}; } 70*67e74705SXin Li { A<0> a = {}; } 71*67e74705SXin Li { A<1> a{1, 1.0}; } 72*67e74705SXin Li { A<1> a = {1, 1.0}; } 73*67e74705SXin Li 74*67e74705SXin Li { F<0> f{}; } 75*67e74705SXin Li { F<0> f = {}; } 76*67e74705SXin Li // Narrowing conversions don't affect viability. The next two choose 77*67e74705SXin Li // the initializer_list constructor. 78*67e74705SXin Li { F<3> f{1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} 79*67e74705SXin Li { F<3> f = {1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} 80*67e74705SXin Li { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; } 81*67e74705SXin Li { F<3> f = {1, 2, 3, 4, 5, 6, 7, 8}; } 82*67e74705SXin Li { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; } 83*67e74705SXin Li { F<3> f{1, 2}; } 84*67e74705SXin Li 85*67e74705SXin Li { D<0> d{1, 2, 3}; } 86*67e74705SXin Li { D<1> d{1.0, 2.0, 3.0}; } 87*67e74705SXin Li { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}} 88*67e74705SXin Li 89*67e74705SXin Li { E<0> e{1, 2}; } 90*67e74705SXin Li } 91*67e74705SXin Li explicit_implicit()92*67e74705SXin Li void explicit_implicit() { 93*67e74705SXin Li { X1 x{0}; } 94*67e74705SXin Li { X1 x = {0}; } 95*67e74705SXin Li { X2 x{0}; } 96*67e74705SXin Li { X2 x = {0}; } // expected-error {{constructor is explicit}} 97*67e74705SXin Li } 98*67e74705SXin Li 99*67e74705SXin Li struct C { 100*67e74705SXin Li C(); 101*67e74705SXin Li C(int, double); 102*67e74705SXin Li C(int, int); 103*67e74705SXin Li 104*67e74705SXin Li int operator[](C); 105*67e74705SXin Li }; 106*67e74705SXin Li function_call()107*67e74705SXin Li C function_call() { 108*67e74705SXin Li void takes_C(C); 109*67e74705SXin Li takes_C({1, 1.0}); 110*67e74705SXin Li 111*67e74705SXin Li C c; 112*67e74705SXin Li c[{1, 1.0}]; 113*67e74705SXin Li 114*67e74705SXin Li return {1, 1.0}; 115*67e74705SXin Li } 116*67e74705SXin Li inline_init()117*67e74705SXin Li void inline_init() { 118*67e74705SXin Li (void) C{1, 1.0}; 119*67e74705SXin Li (void) new C{1, 1.0}; 120*67e74705SXin Li (void) A<1>{1, 1.0}; 121*67e74705SXin Li (void) new A<1>{1, 1.0}; 122*67e74705SXin Li } 123*67e74705SXin Li 124*67e74705SXin Li struct B { // expected-note 2 {{candidate constructor}} 125*67e74705SXin Li B(C, int, C); // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'objects::C'}} 126*67e74705SXin Li }; 127*67e74705SXin Li nested_init()128*67e74705SXin Li void nested_init() { 129*67e74705SXin Li B b1{{1, 1.0}, 2, {3, 4}}; 130*67e74705SXin Li B b2{{1, 1.0, 4}, 2, {3, 4}}; // expected-error {{no matching constructor for initialization of 'objects::B'}} 131*67e74705SXin Li } 132*67e74705SXin Li overloaded_call()133*67e74705SXin Li void overloaded_call() { 134*67e74705SXin Li one ov1(B); // expected-note {{not viable: cannot convert initializer list}} 135*67e74705SXin Li two ov1(C); // expected-note {{not viable: cannot convert initializer list}} 136*67e74705SXin Li 137*67e74705SXin Li static_assert(sizeof(ov1({})) == sizeof(two), "bad overload"); 138*67e74705SXin Li static_assert(sizeof(ov1({1, 2})) == sizeof(two), "bad overload"); 139*67e74705SXin Li static_assert(sizeof(ov1({{1, 1.0}, 2, {3, 4}})) == sizeof(one), "bad overload"); 140*67e74705SXin Li 141*67e74705SXin Li ov1({1}); // expected-error {{no matching function}} 142*67e74705SXin Li 143*67e74705SXin Li one ov2(int); 144*67e74705SXin Li two ov2(F<3>); 145*67e74705SXin Li // expected-warning@+1 {{braces around scalar initializer}} 146*67e74705SXin Li static_assert(sizeof(ov2({1})) == sizeof(one), "bad overload"); // list -> int ranks as identity 147*67e74705SXin Li static_assert(sizeof(ov2({1, 2, 3})) == sizeof(two), "bad overload"); // list -> F only viable 148*67e74705SXin Li } 149*67e74705SXin Li 150*67e74705SXin Li struct G { // expected-note 6 {{not viable}} 151*67e74705SXin Li // This is not an initializer-list constructor. 152*67e74705SXin Li template<typename ...T> 153*67e74705SXin Li G(std::initializer_list<int>, T ...); // expected-note 3 {{not viable}} 154*67e74705SXin Li }; 155*67e74705SXin Li 156*67e74705SXin Li struct H { // expected-note 6 {{not viable}} 157*67e74705SXin Li explicit H(int, int); // expected-note 3 {{not viable}} expected-note {{declared here}} 158*67e74705SXin Li H(int, void*); // expected-note 3 {{not viable}} 159*67e74705SXin Li }; 160*67e74705SXin Li edge_cases()161*67e74705SXin Li void edge_cases() { 162*67e74705SXin Li // invalid (the first phase only considers init-list ctors) 163*67e74705SXin Li // (for the second phase, no constructor is viable) 164*67e74705SXin Li G g1{1, 2, 3}; // expected-error {{no matching constructor}} 165*67e74705SXin Li (void) new G{1, 2, 3}; // expected-error {{no matching constructor}} 166*67e74705SXin Li (void) G{1, 2, 3} // expected-error {{no matching constructor}} 167*67e74705SXin Li 168*67e74705SXin Li // valid (T deduced to <>). 169*67e74705SXin Li G g2({1, 2, 3}); 170*67e74705SXin Li (void) new G({1, 2, 3}); 171*67e74705SXin Li (void) G({1, 2, 3}); 172*67e74705SXin Li 173*67e74705SXin Li // invalid 174*67e74705SXin Li H h1({1, 2}); // expected-error {{no matching constructor}} 175*67e74705SXin Li (void) new H({1, 2}); // expected-error {{no matching constructor}} 176*67e74705SXin Li // FIXME: Bad diagnostic, mentions void type instead of init list. 177*67e74705SXin Li (void) H({1, 2}); // expected-error {{no matching conversion}} 178*67e74705SXin Li 179*67e74705SXin Li // valid (by copy constructor). 180*67e74705SXin Li H h2({1, nullptr}); 181*67e74705SXin Li (void) new H({1, nullptr}); 182*67e74705SXin Li (void) H({1, nullptr}); 183*67e74705SXin Li 184*67e74705SXin Li // valid 185*67e74705SXin Li H h3{1, 2}; 186*67e74705SXin Li (void) new H{1, 2}; 187*67e74705SXin Li (void) H{1, 2}; 188*67e74705SXin Li } 189*67e74705SXin Li 190*67e74705SXin Li struct memberinit { 191*67e74705SXin Li H h1{1, nullptr}; 192*67e74705SXin Li H h2 = {1, nullptr}; 193*67e74705SXin Li H h3{1, 1}; 194*67e74705SXin Li H h4 = {1, 1}; // expected-error {{constructor is explicit}} 195*67e74705SXin Li }; 196*67e74705SXin Li } 197*67e74705SXin Li 198*67e74705SXin Li namespace PR12092 { 199*67e74705SXin Li 200*67e74705SXin Li struct S { 201*67e74705SXin Li S(const char*); 202*67e74705SXin Li }; 203*67e74705SXin Li struct V { 204*67e74705SXin Li template<typename T> V(T, T); 205*67e74705SXin Li void f(std::initializer_list<S>); 206*67e74705SXin Li void f(const V &); 207*67e74705SXin Li }; 208*67e74705SXin Li g()209*67e74705SXin Li void g() { 210*67e74705SXin Li extern V s; 211*67e74705SXin Li s.f({"foo", "bar"}); 212*67e74705SXin Li } 213*67e74705SXin Li 214*67e74705SXin Li } 215*67e74705SXin Li 216*67e74705SXin Li namespace PR12117 { 217*67e74705SXin Li struct A { A(int); }; 218*67e74705SXin Li struct B { B(A); } b{{0}}; //FIXME: non-conformant. Temporary fix until standard resolution. 219*67e74705SXin Li // expected- error {{call to constructor of 'struct B' is ambiguous}} \ 220*67e74705SXin Li // expected- note 2{{candidate is the implicit}} \ 221*67e74705SXin Li // expected- note {{candidate constructor}} 222*67e74705SXin Li struct C { C(int); } c{0}; 223*67e74705SXin Li } 224*67e74705SXin Li 225*67e74705SXin Li namespace PR12167 { 226*67e74705SXin Li template<int N> struct string {}; 227*67e74705SXin Li 228*67e74705SXin Li struct X { 229*67e74705SXin Li X(const char v); 230*67e74705SXin Li template<typename T> bool operator()(T) const; 231*67e74705SXin Li }; 232*67e74705SXin Li g(const string<N> & s,Comparator cmp)233*67e74705SXin Li template<int N, class Comparator> bool g(const string<N>& s, Comparator cmp) { 234*67e74705SXin Li return cmp(s); 235*67e74705SXin Li } f(const string<N> & s)236*67e74705SXin Li template<int N> bool f(const string<N> &s) { 237*67e74705SXin Li return g(s, X{'x'}); 238*67e74705SXin Li } 239*67e74705SXin Li 240*67e74705SXin Li bool s = f(string<1>()); 241*67e74705SXin Li } 242*67e74705SXin Li 243*67e74705SXin Li namespace PR12257_PR12241 { 244*67e74705SXin Li struct command_pair 245*67e74705SXin Li { 246*67e74705SXin Li command_pair(int, int); 247*67e74705SXin Li }; 248*67e74705SXin Li 249*67e74705SXin Li struct command_map 250*67e74705SXin Li { 251*67e74705SXin Li command_map(std::initializer_list<command_pair>); 252*67e74705SXin Li }; 253*67e74705SXin Li 254*67e74705SXin Li struct generator_pair 255*67e74705SXin Li { 256*67e74705SXin Li generator_pair(const command_map); 257*67e74705SXin Li }; 258*67e74705SXin Li 259*67e74705SXin Li // 5 levels: init list, gen_pair, command_map, init list, command_pair 260*67e74705SXin Li const std::initializer_list<generator_pair> x = {{{{{3, 4}}}}}; 261*67e74705SXin Li 262*67e74705SXin Li // 4 levels: init list, gen_pair, command_map via init list, command_pair 263*67e74705SXin Li const std::initializer_list<generator_pair> y = {{{{1, 2}}}}; 264*67e74705SXin Li } 265*67e74705SXin Li 266*67e74705SXin Li namespace PR12120 { 267*67e74705SXin Li struct A { explicit A(int); A(float); }; // expected-note {{declared here}} 268*67e74705SXin Li A a = { 0 }; // expected-error {{constructor is explicit}} 269*67e74705SXin Li 270*67e74705SXin Li struct B { explicit B(short); B(long); }; // expected-note 2 {{candidate}} 271*67e74705SXin Li B b = { 0 }; // expected-error {{ambiguous}} 272*67e74705SXin Li } 273*67e74705SXin Li 274*67e74705SXin Li namespace PR12498 { 275*67e74705SXin Li class ArrayRef; // expected-note{{forward declaration}} 276*67e74705SXin Li 277*67e74705SXin Li struct C { 278*67e74705SXin Li void foo(const ArrayRef&); // expected-note{{passing argument to parameter here}} 279*67e74705SXin Li }; 280*67e74705SXin Li bar(C * c)281*67e74705SXin Li static void bar(C* c) 282*67e74705SXin Li { 283*67e74705SXin Li c->foo({ nullptr, 1 }); // expected-error{{initialization of incomplete type 'const PR12498::ArrayRef'}} 284*67e74705SXin Li } 285*67e74705SXin Li } 286*67e74705SXin Li 287*67e74705SXin Li namespace explicit_default { 288*67e74705SXin Li struct A { 289*67e74705SXin Li explicit A(); // expected-note{{here}} 290*67e74705SXin Li }; 291*67e74705SXin Li A a {}; // ok 292*67e74705SXin Li // This is copy-list-initialization, and we choose an explicit constructor 293*67e74705SXin Li // (even though we do so via value-initialization), so the initialization is 294*67e74705SXin Li // ill-formed. 295*67e74705SXin Li A b = {}; // expected-error{{chosen constructor is explicit}} 296*67e74705SXin Li } 297*67e74705SXin Li 298*67e74705SXin Li namespace init_list_default { 299*67e74705SXin Li struct A { 300*67e74705SXin Li A(std::initializer_list<int>); 301*67e74705SXin Li }; 302*67e74705SXin Li A a {}; // calls initializer list constructor 303*67e74705SXin Li 304*67e74705SXin Li struct B { 305*67e74705SXin Li B(); 306*67e74705SXin Li B(std::initializer_list<int>) = delete; 307*67e74705SXin Li }; 308*67e74705SXin Li B b {}; // calls default constructor 309*67e74705SXin Li } 310*67e74705SXin Li 311*67e74705SXin Li // PR13470, <rdar://problem/11974632> 312*67e74705SXin Li namespace PR13470 { 313*67e74705SXin Li struct W { 314*67e74705SXin Li explicit W(int); // expected-note {{here}} 315*67e74705SXin Li }; 316*67e74705SXin Li 317*67e74705SXin Li struct X { 318*67e74705SXin Li X(const X&) = delete; // expected-note 3 {{here}} 319*67e74705SXin Li X(int); 320*67e74705SXin Li }; 321*67e74705SXin Li call(Fn f)322*67e74705SXin Li template<typename T, typename Fn> void call(Fn f) { 323*67e74705SXin Li f({1}); // expected-error {{constructor is explicit}} 324*67e74705SXin Li f(T{1}); // expected-error {{call to deleted constructor}} 325*67e74705SXin Li } 326*67e74705SXin Li 327*67e74705SXin Li void ref_w(const W &); // expected-note 2 {{not viable}} call_ref_w()328*67e74705SXin Li void call_ref_w() { 329*67e74705SXin Li ref_w({1}); // expected-error {{no matching function}} 330*67e74705SXin Li ref_w(W{1}); 331*67e74705SXin Li call<W>(ref_w); // expected-note {{instantiation of}} 332*67e74705SXin Li } 333*67e74705SXin Li 334*67e74705SXin Li void ref_x(const X &); call_ref_x()335*67e74705SXin Li void call_ref_x() { 336*67e74705SXin Li ref_x({1}); 337*67e74705SXin Li ref_x(X{1}); 338*67e74705SXin Li call<X>(ref_x); // ok 339*67e74705SXin Li } 340*67e74705SXin Li 341*67e74705SXin Li void val_x(X); // expected-note 2 {{parameter}} call_val_x()342*67e74705SXin Li void call_val_x() { 343*67e74705SXin Li val_x({1}); 344*67e74705SXin Li val_x(X{1}); // expected-error {{call to deleted constructor}} 345*67e74705SXin Li call<X>(val_x); // expected-note {{instantiation of}} 346*67e74705SXin Li } 347*67e74705SXin Li 348*67e74705SXin Li template<typename T> 349*67e74705SXin Li struct Y { 350*67e74705SXin Li X x{1}; fPR13470::Y351*67e74705SXin Li void f() { X x{1}; } hPR13470::Y352*67e74705SXin Li void h() { 353*67e74705SXin Li ref_w({1}); // expected-error {{no matching function}} 354*67e74705SXin Li ref_w(W{1}); 355*67e74705SXin Li ref_x({1}); 356*67e74705SXin Li ref_x(X{1}); 357*67e74705SXin Li val_x({1}); 358*67e74705SXin Li val_x(X{1}); // expected-error {{call to deleted constructor}} 359*67e74705SXin Li } YPR13470::Y360*67e74705SXin Li Y() {} YPR13470::Y361*67e74705SXin Li Y(int) : x{1} {} 362*67e74705SXin Li }; 363*67e74705SXin Li 364*67e74705SXin Li Y<int> yi; 365*67e74705SXin Li Y<int> yi2(0); g()366*67e74705SXin Li void g() { 367*67e74705SXin Li yi.f(); 368*67e74705SXin Li yi.h(); // ok, all diagnostics produced in template definition 369*67e74705SXin Li } 370*67e74705SXin Li } 371*67e74705SXin Li 372*67e74705SXin Li namespace PR19729 { 373*67e74705SXin Li struct A { 374*67e74705SXin Li A(int); 375*67e74705SXin Li A(const A&) = delete; 376*67e74705SXin Li }; 377*67e74705SXin Li struct B { 378*67e74705SXin Li void *operator new(std::size_t, A); 379*67e74705SXin Li }; 380*67e74705SXin Li B *p = new ({123}) B; 381*67e74705SXin Li } 382*67e74705SXin Li 383*67e74705SXin Li namespace PR11410 { 384*67e74705SXin Li struct A { 385*67e74705SXin Li A() = delete; // expected-note 2{{deleted here}} 386*67e74705SXin Li A(int); 387*67e74705SXin Li }; 388*67e74705SXin Li 389*67e74705SXin Li A a[3] = { 390*67e74705SXin Li {1}, {2} 391*67e74705SXin Li }; // expected-error {{call to deleted constructor}} \ 392*67e74705SXin Li expected-note {{in implicit initialization of array element 2 with omitted initializer}} 393*67e74705SXin Li 394*67e74705SXin Li struct B { 395*67e74705SXin Li A a; // expected-note {{in implicit initialization of field 'a'}} 396*67e74705SXin Li } b = { 397*67e74705SXin Li }; // expected-error {{call to deleted constructor}} 398*67e74705SXin Li 399*67e74705SXin Li struct C { 400*67e74705SXin Li C(int = 0); // expected-note 2{{candidate}} 401*67e74705SXin Li C(float = 0); // expected-note 2{{candidate}} 402*67e74705SXin Li }; 403*67e74705SXin Li C c[3] = { 404*67e74705SXin Li 0, 1 405*67e74705SXin Li }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 2}} 406*67e74705SXin Li C c2[3] = { 407*67e74705SXin Li [0] = 1, [2] = 3 408*67e74705SXin Li }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 1}} 409*67e74705SXin Li } 410