cxx0x-initializer-constructor.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // RUN: %clang_cc1 -std=c++0x -fsyntax-only -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 2 {{candidate constructor}}
  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. // FIXME: Emit narrowing conversion errors.
  63. { F<3> f{1, 1.0}; } // xpected-error {{narrowing conversion}}
  64. { F<3> f = {1, 1.0}; } // xpected-error {{narrowing conversion}}
  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, 3, 4, 5, 6, 7, 8}; }
  68. { F<3> f{1, 2}; }
  69. { D<0> d{1, 2, 3}; }
  70. { D<1> d{1.0, 2.0, 3.0}; }
  71. { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}}
  72. { E<0> e{1, 2}; }
  73. }
  74. void explicit_implicit() {
  75. { X1 x{0}; }
  76. { X1 x = {0}; }
  77. { X2 x{0}; }
  78. { X2 x = {0}; } // expected-error {{no matching constructor}}
  79. }
  80. struct C {
  81. C();
  82. C(int, double);
  83. C(int, int);
  84. int operator[](C);
  85. };
  86. C function_call() {
  87. void takes_C(C);
  88. takes_C({1, 1.0});
  89. C c;
  90. c[{1, 1.0}];
  91. return {1, 1.0};
  92. }
  93. void inline_init() {
  94. (void) C{1, 1.0};
  95. (void) new C{1, 1.0};
  96. (void) A<1>{1, 1.0};
  97. (void) new A<1>{1, 1.0};
  98. }
  99. struct B { // expected-note 2 {{candidate constructor}}
  100. B(C, int, C); // expected-note {{candidate constructor not viable: cannot convert initializer list argument to 'objects::C'}}
  101. };
  102. void nested_init() {
  103. B b1{{1, 1.0}, 2, {3, 4}};
  104. B b2{{1, 1.0, 4}, 2, {3, 4}}; // expected-error {{no matching constructor for initialization of 'objects::B'}}
  105. }
  106. void overloaded_call() {
  107. one ov1(B); // expected-note {{not viable: cannot convert initializer list}}
  108. two ov1(C); // expected-note {{not viable: cannot convert initializer list}}
  109. static_assert(sizeof(ov1({})) == sizeof(two), "bad overload");
  110. static_assert(sizeof(ov1({1, 2})) == sizeof(two), "bad overload");
  111. static_assert(sizeof(ov1({{1, 1.0}, 2, {3, 4}})) == sizeof(one), "bad overload");
  112. ov1({1}); // expected-error {{no matching function}}
  113. one ov2(int);
  114. two ov2(F<3>);
  115. static_assert(sizeof(ov2({1})) == sizeof(one), "bad overload"); // list -> int ranks as identity
  116. static_assert(sizeof(ov2({1, 2, 3})) == sizeof(two), "bad overload"); // list -> F only viable
  117. }
  118. struct G { // expected-note 6 {{not viable}}
  119. // This is not an initializer-list constructor.
  120. template<typename ...T>
  121. G(std::initializer_list<int>, T ...); // expected-note 3 {{not viable}}
  122. };
  123. struct H { // expected-note 8 {{not viable}}
  124. explicit H(int, int); // expected-note 3 {{not viable}}
  125. H(int, void*); // expected-note 4 {{not viable}}
  126. };
  127. void edge_cases() {
  128. // invalid (the first phase only considers init-list ctors)
  129. // (for the second phase, no constructor is viable)
  130. G g1{1, 2, 3}; // expected-error {{no matching constructor}}
  131. (void) new G{1, 2, 3}; // expected-error {{no matching constructor}}
  132. (void) G{1, 2, 3} // expected-error {{no matching constructor}}
  133. // valid (T deduced to <>).
  134. G g2({1, 2, 3});
  135. (void) new G({1, 2, 3});
  136. (void) G({1, 2, 3});
  137. // invalid
  138. H h1({1, 2}); // expected-error {{no matching constructor}}
  139. (void) new H({1, 2}); // expected-error {{no matching constructor}}
  140. // FIXME: Bad diagnostic, mentions void type instead of init list.
  141. (void) H({1, 2}); // expected-error {{no matching conversion}}
  142. // valid (by copy constructor).
  143. H h2({1, nullptr});
  144. (void) new H({1, nullptr});
  145. (void) H({1, nullptr});
  146. // valid
  147. H h3{1, 2};
  148. (void) new H{1, 2};
  149. (void) H{1, 2};
  150. }
  151. struct memberinit {
  152. H h1{1, nullptr};
  153. H h2 = {1, nullptr};
  154. H h3{1, 1};
  155. H h4 = {1, 1}; // expected-error {{no matching constructor}}
  156. };
  157. }
  158. namespace PR12092 {
  159. struct S {
  160. S(const char*);
  161. };
  162. struct V {
  163. template<typename T> V(T, T);
  164. void f(std::initializer_list<S>);
  165. void f(const V &);
  166. };
  167. void g() {
  168. extern V s;
  169. s.f({"foo", "bar"});
  170. }
  171. }
  172. namespace PR12117 {
  173. struct A { A(int); };
  174. struct B { B(A); } b{{0}};
  175. struct C { C(int); } c{0};
  176. }
  177. namespace PR12167 {
  178. template<int N> struct string {};
  179. struct X {
  180. X(const char v);
  181. template<typename T> bool operator()(T) const;
  182. };
  183. template<int N, class Comparator> bool g(const string<N>& s, Comparator cmp) {
  184. return cmp(s);
  185. }
  186. template<int N> bool f(const string<N> &s) {
  187. return g(s, X{'x'});
  188. }
  189. bool s = f(string<1>());
  190. }
  191. namespace PR12257 {
  192. struct command_pair
  193. {
  194. command_pair(int, int);
  195. };
  196. struct command_map
  197. {
  198. command_map(std::initializer_list<command_pair>);
  199. };
  200. struct generator_pair
  201. {
  202. generator_pair(const command_map);
  203. };
  204. const std::initializer_list<generator_pair> x =
  205. {
  206. {
  207. {
  208. {
  209. {3, 4}
  210. }
  211. }
  212. }
  213. };
  214. }