cxx0x-initializer-constructor.cpp 10 KB

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