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); }; |
| 41 | struct X2 { explicit X2(int); }; // expected-note 2 {{candidate constructor}} |
| 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. |
| 78 | // FIXME: Emit narrowing conversion errors. |
| 79 | { F<3> f{1, 1.0}; } // xpected-error {{narrowing conversion}} |
| 80 | { F<3> f = {1, 1.0}; } // xpected-error {{narrowing conversion}} |
| 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, 3, 4, 5, 6, 7, 8}; } |
| 84 | { F<3> f{1, 2}; } |
| 85 | |
| 86 | { D<0> d{1, 2, 3}; } |
| 87 | { D<1> d{1.0, 2.0, 3.0}; } |
| 88 | { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}} |
| 89 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 90 | { E<0> e{1, 2}; } |
| 91 | } |
| 92 | |
| 93 | void explicit_implicit() { |
| 94 | { X1 x{0}; } |
| 95 | { X1 x = {0}; } |
| 96 | { X2 x{0}; } |
| 97 | { X2 x = {0}; } // expected-error {{no matching constructor}} |
| 98 | } |
| 99 | |
| 100 | struct C { |
| 101 | C(); |
| 102 | C(int, double); |
| 103 | C(int, int); |
| 104 | |
| 105 | int operator[](C); |
| 106 | }; |
| 107 | |
| 108 | C function_call() { |
| 109 | void takes_C(C); |
| 110 | takes_C({1, 1.0}); |
| 111 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 112 | C c; |
| 113 | c[{1, 1.0}]; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 114 | |
| 115 | return {1, 1.0}; |
| 116 | } |
| 117 | |
| 118 | void inline_init() { |
Sebastian Redl | 62f13c9 | 2011-12-22 18:58:29 +0000 | [diff] [blame] | 119 | (void) C{1, 1.0}; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 120 | (void) new C{1, 1.0}; |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 121 | (void) A<1>{1, 1.0}; |
| 122 | (void) new A<1>{1, 1.0}; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 125 | struct B { // expected-note 2 {{candidate constructor}} |
| 126 | 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] | 127 | }; |
| 128 | |
| 129 | void nested_init() { |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 130 | B b1{{1, 1.0}, 2, {3, 4}}; |
| 131 | B b2{{1, 1.0, 4}, 2, {3, 4}}; // expected-error {{no matching constructor for initialization of 'objects::B'}} |
| 132 | } |
| 133 | |
| 134 | void overloaded_call() { |
| 135 | one ov1(B); // expected-note {{not viable: cannot convert initializer list}} |
| 136 | two ov1(C); // expected-note {{not viable: cannot convert initializer list}} |
| 137 | |
| 138 | static_assert(sizeof(ov1({})) == sizeof(two), "bad overload"); |
| 139 | static_assert(sizeof(ov1({1, 2})) == sizeof(two), "bad overload"); |
| 140 | static_assert(sizeof(ov1({{1, 1.0}, 2, {3, 4}})) == sizeof(one), "bad overload"); |
| 141 | |
| 142 | ov1({1}); // expected-error {{no matching function}} |
Sebastian Redl | 56a0428 | 2012-02-11 23:51:08 +0000 | [diff] [blame] | 143 | |
| 144 | one ov2(int); |
| 145 | two ov2(F<3>); |
| 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 |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 148 | } |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 149 | |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 150 | struct G { // expected-note 6 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 151 | // This is not an initializer-list constructor. |
| 152 | template<typename ...T> |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 153 | G(std::initializer_list<int>, T ...); // expected-note 3 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 156 | struct H { // expected-note 6 {{not viable}} |
| 157 | explicit H(int, int); // expected-note 3 {{not viable}} |
| 158 | H(int, void*); // expected-note 3 {{not viable}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | void edge_cases() { |
| 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}} |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 165 | (void) new G{1, 2, 3}; // expected-error {{no matching constructor}} |
| 166 | (void) G{1, 2, 3} // expected-error {{no matching constructor}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 167 | |
| 168 | // valid (T deduced to <>). |
| 169 | G g2({1, 2, 3}); |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 170 | (void) new G({1, 2, 3}); |
| 171 | (void) G({1, 2, 3}); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 172 | |
| 173 | // invalid |
| 174 | H h1({1, 2}); // expected-error {{no matching constructor}} |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 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}} |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 178 | |
| 179 | // valid (by copy constructor). |
| 180 | H h2({1, nullptr}); |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 181 | (void) new H({1, nullptr}); |
| 182 | (void) H({1, nullptr}); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 183 | |
| 184 | // valid |
| 185 | H h3{1, 2}; |
Sebastian Redl | 20ff0e2 | 2012-02-13 19:55:43 +0000 | [diff] [blame] | 186 | (void) new H{1, 2}; |
| 187 | (void) H{1, 2}; |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 188 | } |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 189 | } |