1 // RUN: %clang_cc1 -std=c++0x -fsyntax-only -fexceptions -verify %s
3 struct one { char c[1]; };
4 struct two { char c[2]; };
7 typedef decltype(sizeof(int)) size_t;
9 // libc++'s implementation
11 class initializer_list
16 initializer_list(const _E* __b, size_t __s)
22 typedef _E value_type;
23 typedef const _E& reference;
24 typedef const _E& const_reference;
25 typedef size_t size_type;
27 typedef const _E* iterator;
28 typedef const _E* const_iterator;
30 initializer_list() : __begin_(nullptr), __size_(0) {}
32 size_t size() const {return __size_;}
33 const _E* begin() const {return __begin_;}
34 const _E* end() const {return __begin_ + __size_;}
40 struct X1 { X1(int); };
41 struct X2 { explicit X2(int); }; // expected-note {{constructor declared here}}
45 A() { static_assert(N == 0, ""); }
46 A(int, double) { static_assert(N == 1, ""); }
51 F() { static_assert(N == 0, ""); }
52 F(int, double) { static_assert(N == 1, ""); }
53 F(std::initializer_list<int>) { static_assert(N == 3, ""); }
58 D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}}
59 D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}}
64 E(int, int) { static_assert(N == 0, ""); }
65 E(X1, int) { static_assert(N == 1, ""); }
68 void overload_resolution() {
72 { A<1> a = {1, 1.0}; }
76 // Narrowing conversions don't affect viability. The next two choose
77 // the initializer_list constructor.
78 { F<3> f{1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
79 { F<3> f = {1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
80 { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; }
81 { F<3> f = {1, 2, 3, 4, 5, 6, 7, 8}; }
82 { F<3> f{1, 2, 3, 4, 5, 6, 7, 8}; }
86 { D<1> d{1.0, 2.0, 3.0}; }
87 { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}}
92 void explicit_implicit() {
96 { X2 x = {0}; } // expected-error {{constructor is explicit}}
119 (void) new C{1, 1.0};
121 (void) new A<1>{1, 1.0};
124 struct B { // expected-note 2 {{candidate constructor}}
125 B(C, int, C); // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'objects::C'}}
129 B b1{{1, 1.0}, 2, {3, 4}};
130 B b2{{1, 1.0, 4}, 2, {3, 4}}; // expected-error {{no matching constructor for initialization of 'objects::B'}}
133 void overloaded_call() {
134 one ov1(B); // expected-note {{not viable: cannot convert initializer list}}
135 two ov1(C); // expected-note {{not viable: cannot convert initializer list}}
137 static_assert(sizeof(ov1({})) == sizeof(two), "bad overload");
138 static_assert(sizeof(ov1({1, 2})) == sizeof(two), "bad overload");
139 static_assert(sizeof(ov1({{1, 1.0}, 2, {3, 4}})) == sizeof(one), "bad overload");
141 ov1({1}); // expected-error {{no matching function}}
145 // expected-warning@+1 {{braces around scalar initializer}}
146 static_assert(sizeof(ov2({1})) == sizeof(one), "bad overload"); // list -> int ranks as identity
147 static_assert(sizeof(ov2({1, 2, 3})) == sizeof(two), "bad overload"); // list -> F only viable
150 struct G { // expected-note 6 {{not viable}}
151 // This is not an initializer-list constructor.
152 template<typename ...T>
153 G(std::initializer_list<int>, T ...); // expected-note 3 {{not viable}}
156 struct H { // expected-note 6 {{not viable}}
157 explicit H(int, int); // expected-note 3 {{not viable}} expected-note {{declared here}}
158 H(int, void*); // expected-note 3 {{not viable}}
162 // invalid (the first phase only considers init-list ctors)
163 // (for the second phase, no constructor is viable)
164 G g1{1, 2, 3}; // expected-error {{no matching constructor}}
165 (void) new G{1, 2, 3}; // expected-error {{no matching constructor}}
166 (void) G{1, 2, 3} // expected-error {{no matching constructor}}
168 // valid (T deduced to <>).
170 (void) new G({1, 2, 3});
174 H h1({1, 2}); // expected-error {{no matching constructor}}
175 (void) new H({1, 2}); // expected-error {{no matching constructor}}
176 // FIXME: Bad diagnostic, mentions void type instead of init list.
177 (void) H({1, 2}); // expected-error {{no matching conversion}}
179 // valid (by copy constructor).
181 (void) new H({1, nullptr});
182 (void) H({1, nullptr});
194 H h4 = {1, 1}; // expected-error {{constructor is explicit}}
204 template<typename T> V(T, T);
205 void f(std::initializer_list<S>);
217 struct A { A(int); };
218 struct B { B(A); } b{{0}}; //FIXME: non-conformant. Temporary fix until standard resolution.
219 // expected- error {{call to constructor of 'struct B' is ambiguous}} \
220 // expected- note 2{{candidate is the implicit}} \
221 // expected- note {{candidate constructor}}
222 struct C { C(int); } c{0};
226 template<int N> struct string {};
230 template<typename T> bool operator()(T) const;
233 template<int N, class Comparator> bool g(const string<N>& s, Comparator cmp) {
236 template<int N> bool f(const string<N> &s) {
240 bool s = f(string<1>());
243 namespace PR12257_PR12241 {
246 command_pair(int, int);
251 command_map(std::initializer_list<command_pair>);
254 struct generator_pair
256 generator_pair(const command_map);
259 // 5 levels: init list, gen_pair, command_map, init list, command_pair
260 const std::initializer_list<generator_pair> x = {{{{{3, 4}}}}};
262 // 4 levels: init list, gen_pair, command_map via init list, command_pair
263 const std::initializer_list<generator_pair> y = {{{{1, 2}}}};
267 struct A { explicit A(int); A(float); }; // expected-note {{declared here}}
268 A a = { 0 }; // expected-error {{constructor is explicit}}
270 struct B { explicit B(short); B(long); }; // expected-note 2 {{candidate}}
271 B b = { 0 }; // expected-error {{ambiguous}}
275 class ArrayRef; // expected-note{{forward declaration}}
278 void foo(const ArrayRef&); // expected-note{{passing argument to parameter here}}
281 static void bar(C* c)
283 c->foo({ nullptr, 1 }); // expected-error{{initialization of incomplete type 'const PR12498::ArrayRef'}}
287 namespace explicit_default {
289 explicit A(); // expected-note{{here}}
292 // This is copy-list-initialization, and we choose an explicit constructor
293 // (even though we do so via value-initialization), so the initialization is
295 A b = {}; // expected-error{{chosen constructor is explicit}}
298 namespace init_list_default {
300 A(std::initializer_list<int>);
302 A a {}; // calls initializer list constructor
306 B(std::initializer_list<int>) = delete;
308 B b {}; // calls default constructor
311 // PR13470, <rdar://problem/11974632>
314 explicit W(int); // expected-note {{here}}
318 X(const X&) = delete; // expected-note 3 {{here}}
322 template<typename T, typename Fn> void call(Fn f) {
323 f({1}); // expected-error {{constructor is explicit}}
324 f(T{1}); // expected-error {{call to deleted constructor}}
327 void ref_w(const W &); // expected-note 2 {{not viable}}
329 ref_w({1}); // expected-error {{no matching function}}
331 call<W>(ref_w); // expected-note {{instantiation of}}
334 void ref_x(const X &);
338 call<X>(ref_x); // ok
341 void val_x(X); // expected-note 2 {{parameter}}
344 val_x(X{1}); // expected-error {{call to deleted constructor}}
345 call<X>(val_x); // expected-note {{instantiation of}}
353 ref_w({1}); // expected-error {{no matching function}}
358 val_x(X{1}); // expected-error {{call to deleted constructor}}
368 yi.h(); // ok, all diagnostics produced in template definition
375 A(const A&) = delete;
378 void *operator new(std::size_t, A);
380 B *p = new ({123}) B;
385 A() = delete; // expected-note 2{{deleted here}}
391 }; // expected-error {{call to deleted constructor}} \
392 expected-note {{in implicit initialization of array element 2 with omitted initializer}}
395 A a; // expected-note {{in implicit initialization of field 'a'}}
397 }; // expected-error {{call to deleted constructor}}
400 C(int = 0); // expected-note 2{{candidate}}
401 C(float = 0); // expected-note 2{{candidate}}
405 }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 2}}
408 }; // expected-error {{ambiguous}} expected-note {{in implicit initialization of array element 1}}