Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame^] | 1 | // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 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 | |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 43 | int function_call() { |
| 44 | void takes_int(int); |
| 45 | takes_int({1}); |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void inline_init() { |
| 49 | (void) int{1}; |
| 50 | (void) new int{1}; |
| 51 | } |
| 52 | |
| 53 | void initializer_list() { |
Sebastian Redl | 064e236 | 2011-06-05 12:23:21 +0000 | [diff] [blame] | 54 | std::initializer_list<int> il = { 1, 2, 3 }; |
| 55 | std::initializer_list<double> dl = { 1.0, 2.0, 3 }; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 56 | auto l = {1, 2, 3, 4}; |
| 57 | static_assert(same_type<decltype(l), std::initializer_list<int>>::value, ""); |
Sebastian Redl | 064e236 | 2011-06-05 12:23:21 +0000 | [diff] [blame] | 58 | auto bl = {1, 2.0}; // expected-error {{cannot deduce}} |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 59 | |
| 60 | for (int i : {1, 2, 3, 4}) {} |
| 61 | } |
| 62 | |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | namespace objects { |
| 66 | |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame] | 67 | struct X1 { X1(int); }; |
| 68 | struct X2 { explicit X2(int); }; |
| 69 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 70 | template <int N> |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 71 | struct A { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 72 | A() { static_assert(N == 0, ""); } |
| 73 | A(int, double) { static_assert(N == 1, ""); } |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 74 | A(std::initializer_list<int>) { static_assert(N == 3, ""); } |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame] | 77 | template <int N> |
| 78 | struct D { |
| 79 | D(std::initializer_list<int>) { static_assert(N == 0, ""); } // expected-note 1 {{candidate}} |
| 80 | D(std::initializer_list<double>) { static_assert(N == 1, ""); } // expected-note 1 {{candidate}} |
| 81 | }; |
| 82 | |
| 83 | template <int N> |
| 84 | struct E { |
| 85 | E(int, int) { static_assert(N == 0, ""); } |
| 86 | E(X1, int) { static_assert(N == 1, ""); } |
| 87 | }; |
| 88 | |
| 89 | void overload_resolution() { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 90 | { A<0> a{}; } |
| 91 | { A<0> a = {}; } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame] | 92 | // Narrowing conversions don't affect viability. The next two choose |
| 93 | // the initializer_list constructor. |
| 94 | { A<3> a{1, 1.0}; } // expected-error {{narrowing conversion}} |
| 95 | { A<3> a = {1, 1.0}; } // expected-error {{narrowing conversion}} |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 96 | { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; } |
| 97 | { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; } |
| 98 | { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; } |
| 99 | { A<3> a{1, 2}; } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame] | 100 | |
| 101 | { D<0> d{1, 2, 3}; } |
| 102 | { D<1> d{1.0, 2.0, 3.0}; } |
| 103 | { D<-1> d{1, 2.0}; } // expected-error {{ambiguous}} |
| 104 | |
| 105 | { E<0> e{1, 2}; } |
| 106 | } |
| 107 | |
| 108 | void explicit_implicit() { |
| 109 | { X1 x{0}; } |
| 110 | { X1 x = {0}; } |
| 111 | { X2 x{0}; } |
| 112 | { X2 x = {0}; } // expected-error {{explicit}} |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 115 | struct C { |
| 116 | C(); |
| 117 | C(int, double); |
| 118 | C(int, int); |
| 119 | C(std::initializer_list<int>); |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 120 | |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 121 | int operator[](C); |
| 122 | }; |
| 123 | |
| 124 | C function_call() { |
| 125 | void takes_C(C); |
| 126 | takes_C({1, 1.0}); |
| 127 | |
| 128 | C c; |
| 129 | c[{1, 1.0}]; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 130 | |
| 131 | return {1, 1.0}; |
| 132 | } |
| 133 | |
| 134 | void inline_init() { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 135 | (void) A<1>{1, 1.0}; |
| 136 | (void) new A<1>{1, 1.0}; |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | struct B { |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 140 | B(C, int, C); |
Sebastian Redl | ce7cd26 | 2011-05-20 21:07:01 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | void nested_init() { |
| 144 | B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}}; |
| 145 | } |
| 146 | } |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 147 | |
| 148 | namespace litb { |
| 149 | |
| 150 | // invalid |
| 151 | struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}} |
| 152 | |
| 153 | // invalid |
| 154 | int a({0}); // expected-error {{}} |
| 155 | |
| 156 | // invalid |
| 157 | int const &b({0}); // expected-error {{}} |
| 158 | |
| 159 | struct C { explicit C(int, int); C(int, long); }; |
| 160 | |
| 161 | // invalid |
| 162 | C c({1, 2}); // expected-error {{}} |
| 163 | |
| 164 | // valid (by copy constructor). |
| 165 | C d({1, 2L}); // expected-error {{}} |
| 166 | |
| 167 | // valid |
| 168 | C e{1, 2}; |
| 169 | |
| 170 | struct B { |
| 171 | template<typename ...T> |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 172 | B(std::initializer_list<int>, T ...); |
Sebastian Redl | 262b62b | 2011-06-05 12:23:08 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | // invalid (the first phase only considers init-list ctors) |
| 176 | // (for the second phase, no constructor is viable) |
| 177 | B f{1, 2, 3}; |
| 178 | |
| 179 | // valid (T deduced to <>). |
| 180 | B g({1, 2, 3}); |
| 181 | |
| 182 | } |
Sebastian Redl | dc998b4 | 2011-07-14 19:08:01 +0000 | [diff] [blame] | 183 | |
| 184 | namespace aggregate { |
| 185 | // Direct list initialization does NOT allow braces to be elided! |
| 186 | struct S { |
| 187 | int ar[2]; |
| 188 | struct T { |
| 189 | int i1; |
| 190 | int i2; |
| 191 | } t; |
| 192 | struct U { |
| 193 | int i1; |
| 194 | } u[2]; |
| 195 | struct V { |
| 196 | int var[2]; |
| 197 | } v; |
| 198 | }; |
| 199 | |
| 200 | void test() { |
| 201 | S s1 = { 1, 2, 3 ,4, 5, 6, 7, 8 }; // no-error |
| 202 | S s2{ {1, 2}, {3, 4}, { {5}, {6} }, { {7, 8} } }; // completely braced |
| 203 | S s3{ 1, 2, 3, 4, 5, 6 }; // xpected-error |
| 204 | S s4{ {1, 2}, {3, 4}, {5, 6}, { {7, 8} } }; // xpected-error |
| 205 | S s5{ {1, 2}, {3, 4}, { {5}, {6} }, {7, 8} }; // xpected-error |
| 206 | } |
| 207 | } |