Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s |
| 2 | // XFAIL: * |
| 3 | |
| 4 | template <typename T, typename U> |
| 5 | struct same_type { static const bool value = false; }; |
| 6 | template <typename T> |
| 7 | struct same_type<T, T> { static const bool value = true; }; |
| 8 | |
| 9 | namespace std { |
| 10 | typedef decltype(sizeof(int)) size_t; |
| 11 | |
| 12 | // libc++'s implementation |
| 13 | template <class _E> |
| 14 | class initializer_list |
| 15 | { |
| 16 | const _E* __begin_; |
| 17 | size_t __size_; |
| 18 | |
| 19 | initializer_list(const _E* __b, size_t __s) |
| 20 | : __begin_(__b), |
| 21 | __size_(__s) |
| 22 | {} |
| 23 | |
| 24 | public: |
| 25 | typedef _E value_type; |
| 26 | typedef const _E& reference; |
| 27 | typedef const _E& const_reference; |
| 28 | typedef size_t size_type; |
| 29 | |
| 30 | typedef const _E* iterator; |
| 31 | typedef const _E* const_iterator; |
| 32 | |
| 33 | initializer_list() : __begin_(nullptr), __size_(0) {} |
| 34 | |
| 35 | size_t size() const {return __size_;} |
| 36 | const _E* begin() const {return __begin_;} |
| 37 | const _E* end() const {return __begin_ + __size_;} |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | namespace integral { |
| 42 | |
| 43 | void initialization() { |
| 44 | { const int a{}; static_assert(a == 0, ""); } |
| 45 | { const int a = {}; static_assert(a == 0, ""); } |
| 46 | { const int a{1}; static_assert(a == 1, ""); } |
| 47 | { const int a = {1}; static_assert(a == 1, ""); } |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 48 | { const int a{1, 2}; } // expected-error {{excess elements}} |
| 49 | { const int a = {1, 2}; } // expected-error {{excess elements}} |
| 50 | { const short a{100000}; } // expected-error {{narrowing conversion}} |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 51 | { const short a = {100000}; } // expected-error {{narrowing conversion}} |
| 52 | } |
| 53 | |
| 54 | int function_call() { |
| 55 | void takes_int(int); |
| 56 | takes_int({1}); |
| 57 | |
| 58 | int ar[10]; |
| 59 | (void) ar[{1}]; // expected-error {{initializer list is illegal with the built-in index operator}} |
| 60 | |
| 61 | return {1}; |
| 62 | } |
| 63 | |
| 64 | void inline_init() { |
| 65 | (void) int{1}; |
| 66 | (void) new int{1}; |
| 67 | } |
| 68 | |
| 69 | void initializer_list() { |
Sebastian Redl | 064e236 | 2011-06-05 12:23:21 +0000 | [diff] [blame] | 70 | std::initializer_list<int> il = { 1, 2, 3 }; |
| 71 | std::initializer_list<double> dl = { 1.0, 2.0, 3 }; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 72 | auto l = {1, 2, 3, 4}; |
| 73 | static_assert(same_type<decltype(l), std::initializer_list<int>>::value, ""); |
Sebastian Redl | 064e236 | 2011-06-05 12:23:21 +0000 | [diff] [blame] | 74 | auto bl = {1, 2.0}; // expected-error {{cannot deduce}} |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 75 | |
| 76 | for (int i : {1, 2, 3, 4}) {} |
| 77 | } |
| 78 | |
| 79 | struct A { |
| 80 | int i; |
| 81 | A() : i{1} {} |
| 82 | }; |
| 83 | |
| 84 | } |
| 85 | |
| 86 | namespace objects { |
| 87 | |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame^] | 88 | struct X1 { X1(int); }; |
| 89 | struct X2 { explicit X2(int); }; |
| 90 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 91 | template <int N> |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 92 | struct A { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 93 | A() { static_assert(N == 0, ""); } |
| 94 | A(int, double) { static_assert(N == 1, ""); } |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 95 | A(std::initializer_list<int>) { static_assert(N == 3, ""); } |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame^] | 98 | template <int N> |
| 99 | struct D { |
| 100 | D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}} |
| 101 | D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}} |
| 102 | }; |
| 103 | |
| 104 | template <int N> |
| 105 | struct E { |
| 106 | E(int, int) { static_assert(N == 0, ""); } |
| 107 | E(X1, int) { static_assert(N == 1, ""); } |
| 108 | }; |
| 109 | |
| 110 | void overload_resolution() { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 111 | { A<0> a{}; } |
| 112 | { A<0> a = {}; } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame^] | 113 | // Narrowing conversions don't affect viability. The next two choose |
| 114 | // the initializer_list constructor. |
| 115 | { A<3> a{1, 1.0}; } // expected-error {{narrowing conversion}} |
| 116 | { A<3> a = {1, 1.0}; } // expected-error {{narrowing conversion}} |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 117 | { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; } |
| 118 | { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; } |
| 119 | { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; } |
| 120 | { A<3> a{1, 2}; } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame^] | 121 | |
| 122 | { D<0> d{1, 2, 3}; } |
| 123 | { D<1> d{1.0, 2.0, 3.0}; } |
| 124 | { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}} |
| 125 | |
| 126 | { E<0> e{1, 2}; } |
| 127 | } |
| 128 | |
| 129 | void explicit_implicit() { |
| 130 | { X1 x{0}; } |
| 131 | { X1 x = {0}; } |
| 132 | { X2 x{0}; } |
| 133 | { X2 x = {0}; } // expected-error {{explicit}} |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 136 | struct C { |
| 137 | C(); |
| 138 | C(int, double); |
| 139 | C(int, int); |
| 140 | C(std::initializer_list<int>); |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 141 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 142 | int operator[](C); |
| 143 | }; |
| 144 | |
| 145 | C function_call() { |
| 146 | void takes_C(C); |
| 147 | takes_C({1, 1.0}); |
| 148 | |
| 149 | C c; |
| 150 | c[{1, 1.0}]; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 151 | |
| 152 | return {1, 1.0}; |
| 153 | } |
| 154 | |
| 155 | void inline_init() { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 156 | (void) A<1>{1, 1.0}; |
| 157 | (void) new A<1>{1, 1.0}; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | struct B { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 161 | B(C, int, C); |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | void nested_init() { |
| 165 | B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}}; |
| 166 | } |
| 167 | } |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 168 | |
| 169 | namespace litb { |
| 170 | |
| 171 | // invalid |
| 172 | struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}} |
| 173 | |
| 174 | // invalid |
| 175 | int a({0}); // expected-error {{}} |
| 176 | |
| 177 | // invalid |
| 178 | int const &b({0}); // expected-error {{}} |
| 179 | |
| 180 | struct C { explicit C(int, int); C(int, long); }; |
| 181 | |
| 182 | // invalid |
| 183 | C c({1, 2}); // expected-error {{}} |
| 184 | |
| 185 | // valid (by copy constructor). |
| 186 | C d({1, 2L}); // expected-error {{}} |
| 187 | |
| 188 | // valid |
| 189 | C e{1, 2}; |
| 190 | |
| 191 | struct B { |
| 192 | template<typename ...T> |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 193 | B(std::initializer_list<int>, T ...); |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | // invalid (the first phase only considers init-list ctors) |
| 197 | // (for the second phase, no constructor is viable) |
| 198 | B f{1, 2, 3}; |
| 199 | |
| 200 | // valid (T deduced to <>). |
| 201 | B g({1, 2, 3}); |
| 202 | |
| 203 | } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame^] | 204 | |
| 205 | namespace aggregate { |
| 206 | // Direct list initialization does NOT allow braces to be elided! |
| 207 | struct S { |
| 208 | int ar[2]; |
| 209 | struct T { |
| 210 | int i1; |
| 211 | int i2; |
| 212 | } t; |
| 213 | struct U { |
| 214 | int i1; |
| 215 | } u[2]; |
| 216 | struct V { |
| 217 | int var[2]; |
| 218 | } v; |
| 219 | }; |
| 220 | |
| 221 | void test() { |
| 222 | S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; // no-error |
| 223 | S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced |
| 224 | S s3{ 1, 2, 3, 4, 5, 6 }; // xpected-error |
| 225 | S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // xpected-error |
| 226 | S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // xpected-error |
| 227 | } |
| 228 | } |