Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s |
| 2 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3 | struct one { char c[1]; }; |
| 4 | struct two { char c[2]; }; |
| 5 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 6 | namespace std { |
| 7 | typedef decltype(sizeof(int)) size_t; |
| 8 | |
| 9 | // libc++'s implementation |
| 10 | template <class _E> |
| 11 | class initializer_list |
| 12 | { |
| 13 | const _E* __begin_; |
| 14 | size_t __size_; |
| 15 | |
| 16 | initializer_list(const _E* __b, size_t __s) |
| 17 | : __begin_(__b), |
| 18 | __size_(__s) |
| 19 | {} |
| 20 | |
| 21 | public: |
| 22 | typedef _E value_type; |
| 23 | typedef const _E& reference; |
| 24 | typedef const _E& const_reference; |
| 25 | typedef size_t size_type; |
| 26 | |
| 27 | typedef const _E* iterator; |
| 28 | typedef const _E* const_iterator; |
| 29 | |
| 30 | initializer_list() : __begin_(nullptr), __size_(0) {} |
| 31 | |
| 32 | size_t size() const {return __size_;} |
| 33 | const _E* begin() const {return __begin_;} |
| 34 | const _E* end() const {return __begin_ + __size_;} |
| 35 | }; |
| 36 | } |
| 37 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 38 | namespace objects { |
| 39 | |
| 40 | struct X1 { X1(int); }; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 41 | struct X2 { explicit X2(int); }; // expected-note {{constructor declared here}} |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 42 | |
| 43 | template <int N> |
| 44 | struct A { |
| 45 | A() { static_assert(N == 0, ""); } |
| 46 | A(int, double) { static_assert(N == 1, ""); } |
| 47 | }; |
| 48 | |
| 49 | template <int N> |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 50 | struct F { |
| 51 | F() { static_assert(N == 0, ""); } |
| 52 | F(int, double) { static_assert(N == 1, ""); } |
| 53 | F(std::initializer_list<int>) { static_assert(N == 3, ""); } |
| 54 | }; |
| 55 | |
| 56 | template <int N> |
| 57 | struct D { |
| 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}} |
| 60 | }; |
| 61 | |
| 62 | template <int N> |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 63 | struct E { |
| 64 | E(int, int) { static_assert(N == 0, ""); } |
| 65 | E(X1, int) { static_assert(N == 1, ""); } |
| 66 | }; |
| 67 | |
| 68 | void overload_resolution() { |
| 69 | { A<0> a{}; } |
| 70 | { A<0> a = {}; } |
| 71 | { A<1> a{1, 1.0}; } |
| 72 | { A<1> a = {1, 1.0}; } |
| 73 | |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 74 | { F<0> f{}; } |
| 75 | { F<0> f = {}; } |
| 76 | // Narrowing conversions don't affect viability. The next two choose |
| 77 | // the initializer_list constructor. |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 78 | { F<3> f{1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{override}} |
| 79 | { F<3> f = {1, 1.0}; } // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{override}} |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 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}; } |
| 83 | { F<3> f{1, 2}; } |
| 84 | |
| 85 | { D<0> d{1, 2, 3}; } |
| 86 | { D<1> d{1.0, 2.0, 3.0}; } |
| 87 | { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}} |
| 88 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 89 | { E<0> e{1, 2}; } |
| 90 | } |
| 91 | |
| 92 | void explicit_implicit() { |
| 93 | { X1 x{0}; } |
| 94 | { X1 x = {0}; } |
| 95 | { X2 x{0}; } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 96 | { X2 x = {0}; } // expected-error {{constructor is explicit}} |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | struct C { |
| 100 | C(); |
| 101 | C(int, double); |
| 102 | C(int, int); |
| 103 | |
| 104 | int operator[](C); |
| 105 | }; |
| 106 | |
| 107 | C function_call() { |
| 108 | void takes_C(C); |
| 109 | takes_C({1, 1.0}); |
| 110 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 111 | C c; |
| 112 | c[{1, 1.0}]; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 113 | |
| 114 | return {1, 1.0}; |
| 115 | } |
| 116 | |
| 117 | void inline_init() { |
Sebastian Redl | 62f13c9 | 2011-12-22 18:58:29 +0000 | [diff] [blame] | 118 | (void) C{1, 1.0}; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 119 | (void) new C{1, 1.0}; |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 120 | (void) A<1>{1, 1.0}; |
| 121 | (void) new A<1>{1, 1.0}; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 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'}} |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | void nested_init() { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 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'}} |
| 131 | } |
| 132 | |
| 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}} |
| 136 | |
| 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"); |
| 140 | |
| 141 | ov1({1}); // expected-error {{no matching function}} |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 142 | |
| 143 | one ov2(int); |
| 144 | two ov2(F<3>); |
| 145 | static_assert(sizeof(ov2({1})) == sizeof(one), "bad overload"); // list -> int ranks as identity |
| 146 | static_assert(sizeof(ov2({1, 2, 3})) == sizeof(two), "bad overload"); // list -> F only viable |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 147 | } |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 148 | |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 149 | struct G { // expected-note 6 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 150 | // This is not an initializer-list constructor. |
| 151 | template<typename ...T> |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 152 | G(std::initializer_list<int>, T ...); // expected-note 3 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 155 | struct H { // expected-note 6 {{not viable}} |
| 156 | explicit H(int, int); // expected-note 3 {{not viable}} expected-note {{declared here}} |
| 157 | H(int, void*); // expected-note 3 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | void edge_cases() { |
| 161 | // invalid (the first phase only considers init-list ctors) |
| 162 | // (for the second phase, no constructor is viable) |
| 163 | G g1{1, 2, 3}; // expected-error {{no matching constructor}} |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 164 | (void) new G{1, 2, 3}; // expected-error {{no matching constructor}} |
| 165 | (void) G{1, 2, 3} // expected-error {{no matching constructor}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 166 | |
| 167 | // valid (T deduced to <>). |
| 168 | G g2({1, 2, 3}); |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 169 | (void) new G({1, 2, 3}); |
| 170 | (void) G({1, 2, 3}); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 171 | |
| 172 | // invalid |
| 173 | H h1({1, 2}); // expected-error {{no matching constructor}} |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 174 | (void) new H({1, 2}); // expected-error {{no matching constructor}} |
| 175 | // FIXME: Bad diagnostic, mentions void type instead of init list. |
| 176 | (void) H({1, 2}); // expected-error {{no matching conversion}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 177 | |
| 178 | // valid (by copy constructor). |
| 179 | H h2({1, nullptr}); |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 180 | (void) new H({1, nullptr}); |
| 181 | (void) H({1, nullptr}); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 182 | |
| 183 | // valid |
| 184 | H h3{1, 2}; |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 185 | (void) new H{1, 2}; |
| 186 | (void) H{1, 2}; |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 187 | } |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 188 | |
| 189 | struct memberinit { |
| 190 | H h1{1, nullptr}; |
| 191 | H h2 = {1, nullptr}; |
| 192 | H h3{1, 1}; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 193 | H h4 = {1, 1}; // expected-error {{constructor is explicit}} |
Sebastian Redl | 33deb35 | 2012-02-22 10:50:08 +0000 | [diff] [blame] | 194 | }; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 195 | } |
Sebastian Redl | adfb535 | 2012-02-27 22:38:26 +0000 | [diff] [blame] | 196 | |
| 197 | namespace PR12092 { |
| 198 | |
| 199 | struct S { |
| 200 | S(const char*); |
| 201 | }; |
| 202 | struct V { |
| 203 | template<typename T> V(T, T); |
| 204 | void f(std::initializer_list<S>); |
| 205 | void f(const V &); |
| 206 | }; |
| 207 | |
| 208 | void g() { |
| 209 | extern V s; |
| 210 | s.f({"foo", "bar"}); |
| 211 | } |
| 212 | |
| 213 | } |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 214 | |
| 215 | namespace PR12117 { |
| 216 | struct A { A(int); }; |
| 217 | struct B { B(A); } b{{0}}; |
| 218 | struct C { C(int); } c{0}; |
| 219 | } |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 220 | |
| 221 | namespace PR12167 { |
| 222 | template<int N> struct string {}; |
| 223 | |
| 224 | struct X { |
| 225 | X(const char v); |
| 226 | template<typename T> bool operator()(T) const; |
| 227 | }; |
| 228 | |
| 229 | template<int N, class Comparator> bool g(const string<N>& s, Comparator cmp) { |
| 230 | return cmp(s); |
| 231 | } |
| 232 | template<int N> bool f(const string<N> &s) { |
| 233 | return g(s, X{'x'}); |
| 234 | } |
| 235 | |
| 236 | bool s = f(string<1>()); |
| 237 | } |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 238 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 239 | namespace PR12257_PR12241 { |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 240 | struct command_pair |
| 241 | { |
| 242 | command_pair(int, int); |
| 243 | }; |
| 244 | |
| 245 | struct command_map |
| 246 | { |
| 247 | command_map(std::initializer_list<command_pair>); |
| 248 | }; |
| 249 | |
| 250 | struct generator_pair |
| 251 | { |
| 252 | generator_pair(const command_map); |
| 253 | }; |
| 254 | |
Sebastian Redl | f78c0f9 | 2012-03-27 18:33:03 +0000 | [diff] [blame] | 255 | // 5 levels: init list, gen_pair, command_map, init list, command_pair |
| 256 | const std::initializer_list<generator_pair> x = {{{{{3, 4}}}}}; |
| 257 | |
| 258 | // 4 levels: init list, gen_pair, command_map via init list, command_pair |
| 259 | const std::initializer_list<generator_pair> y = {{{{1, 2}}}}; |
Sebastian Redl | 1cd89c4 | 2012-03-20 21:24:14 +0000 | [diff] [blame] | 260 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 261 | |
| 262 | namespace PR12120 { |
| 263 | struct A { explicit A(int); A(float); }; // expected-note {{declared here}} |
| 264 | A a = { 0 }; // expected-error {{constructor is explicit}} |
| 265 | |
| 266 | struct B { explicit B(short); B(long); }; // expected-note 2 {{candidate}} |
| 267 | B b = { 0 }; // expected-error {{ambiguous}} |
| 268 | } |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 269 | |
| 270 | namespace PR12498 { |
| 271 | class ArrayRef; // expected-note{{forward declaration}} |
| 272 | |
| 273 | struct C { |
| 274 | void foo(const ArrayRef&); // expected-note{{passing argument to parameter here}} |
| 275 | }; |
| 276 | |
| 277 | static void bar(C* c) |
| 278 | { |
| 279 | c->foo({ nullptr, 1 }); // expected-error{{initialization of incomplete type 'const PR12498::ArrayRef'}} |
| 280 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 281 | } |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 282 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 283 | namespace explicit_default { |
| 284 | struct A { |
| 285 | explicit A(); // expected-note{{here}} |
| 286 | }; |
| 287 | A a {}; // ok |
| 288 | // This is copy-list-initialization, and we choose an explicit constructor |
| 289 | // (even though we do so via value-initialization), so the initialization is |
| 290 | // ill-formed. |
| 291 | A b = {}; // expected-error{{chosen constructor is explicit}} |
| 292 | } |
| 293 | |
| 294 | namespace init_list_default { |
| 295 | struct A { |
| 296 | A(std::initializer_list<int>); |
| 297 | }; |
| 298 | A a {}; // calls initializer list constructor |
| 299 | |
| 300 | struct B { |
| 301 | B(); |
| 302 | B(std::initializer_list<int>) = delete; |
| 303 | }; |
| 304 | B b {}; // calls default constructor |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 305 | } |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 306 | |
| 307 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 308 | // PR13470, <rdar://problem/11974632> |
| 309 | namespace PR13470 { |
| 310 | struct W { |
| 311 | explicit W(int); // expected-note {{here}} |
| 312 | }; |
| 313 | |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 314 | struct X { |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 315 | X(const X&) = delete; // expected-note 3 {{here}} |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 316 | X(int); |
| 317 | }; |
| 318 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 319 | template<typename T, typename Fn> void call(Fn f) { |
| 320 | f({1}); // expected-error {{constructor is explicit}} |
| 321 | f(T{1}); // expected-error {{call to deleted constructor}} |
| 322 | } |
| 323 | |
| 324 | void ref_w(const W &); // expected-note 2 {{not viable}} |
| 325 | void call_ref_w() { |
| 326 | ref_w({1}); // expected-error {{no matching function}} |
| 327 | ref_w(W{1}); |
| 328 | call<W>(ref_w); // expected-note {{instantiation of}} |
| 329 | } |
| 330 | |
| 331 | void ref_x(const X &); |
| 332 | void call_ref_x() { |
| 333 | ref_x({1}); |
| 334 | ref_x(X{1}); |
| 335 | call<X>(ref_x); // ok |
| 336 | } |
| 337 | |
| 338 | void val_x(X); // expected-note 2 {{parameter}} |
| 339 | void call_val_x() { |
| 340 | val_x({1}); |
| 341 | val_x(X{1}); // expected-error {{call to deleted constructor}} |
| 342 | call<X>(val_x); // expected-note {{instantiation of}} |
| 343 | } |
| 344 | |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 345 | template<typename T> |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 346 | struct Y { |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 347 | X x{1}; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 348 | void f() { X x{1}; } |
| 349 | void h() { |
| 350 | ref_w({1}); // expected-error {{no matching function}} |
| 351 | ref_w(W{1}); |
| 352 | ref_x({1}); |
| 353 | ref_x(X{1}); |
| 354 | val_x({1}); |
| 355 | val_x(X{1}); // expected-error {{call to deleted constructor}} |
| 356 | } |
| 357 | Y() {} |
| 358 | Y(int) : x{1} {} |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 359 | }; |
| 360 | |
| 361 | Y<int> yi; |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 362 | Y<int> yi2(0); |
| 363 | void g() { |
| 364 | yi.f(); |
| 365 | yi.h(); // ok, all diagnostics produced in template definition |
| 366 | } |
Douglas Gregor | dd08427 | 2012-09-14 04:20:37 +0000 | [diff] [blame] | 367 | } |