blob: a1891c9c322e39325aebf9b84d713ea3e8252692 [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
Sebastian Redlce7cd262011-05-20 21:07:01 +00002// XFAIL: *
3
4template <typename T, typename U>
5struct same_type { static const bool value = false; };
6template <typename T>
7struct same_type<T, T> { static const bool value = true; };
8
9namespace 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
41namespace integral {
42
Sebastian Redlce7cd262011-05-20 21:07:01 +000043 int function_call() {
44 void takes_int(int);
45 takes_int({1});
Sebastian Redlce7cd262011-05-20 21:07:01 +000046 }
47
48 void inline_init() {
49 (void) int{1};
50 (void) new int{1};
51 }
52
53 void initializer_list() {
Sebastian Redl064e2362011-06-05 12:23:21 +000054 std::initializer_list<int> il = { 1, 2, 3 };
55 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
Sebastian Redlce7cd262011-05-20 21:07:01 +000056 auto l = {1, 2, 3, 4};
57 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
Sebastian Redl064e2362011-06-05 12:23:21 +000058 auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
Sebastian Redlce7cd262011-05-20 21:07:01 +000059
60 for (int i : {1, 2, 3, 4}) {}
61 }
62
Sebastian Redlce7cd262011-05-20 21:07:01 +000063}
64
65namespace objects {
66
Sebastian Redldc998b42011-07-14 19:08:01 +000067 struct X1 { X1(int); };
68 struct X2 { explicit X2(int); };
69
Sebastian Redl262b62b2011-06-05 12:23:08 +000070 template <int N>
Sebastian Redlce7cd262011-05-20 21:07:01 +000071 struct A {
Sebastian Redl262b62b2011-06-05 12:23:08 +000072 A() { static_assert(N == 0, ""); }
73 A(int, double) { static_assert(N == 1, ""); }
Sebastian Redl262b62b2011-06-05 12:23:08 +000074 A(std::initializer_list<int>) { static_assert(N == 3, ""); }
Sebastian Redlce7cd262011-05-20 21:07:01 +000075 };
76
Sebastian Redldc998b42011-07-14 19:08:01 +000077 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 Redl262b62b2011-06-05 12:23:08 +000090 { A<0> a{}; }
91 { A<0> a = {}; }
Sebastian Redldc998b42011-07-14 19:08:01 +000092 // 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 Redl262b62b2011-06-05 12:23:08 +000096 { 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 Redldc998b42011-07-14 19:08:01 +0000100
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 Redlce7cd262011-05-20 21:07:01 +0000113 }
114
Sebastian Redl262b62b2011-06-05 12:23:08 +0000115 struct C {
116 C();
117 C(int, double);
118 C(int, int);
119 C(std::initializer_list<int>);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000120
Sebastian Redl262b62b2011-06-05 12:23:08 +0000121 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 Redlce7cd262011-05-20 21:07:01 +0000130
131 return {1, 1.0};
132 }
133
134 void inline_init() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000135 (void) A<1>{1, 1.0};
136 (void) new A<1>{1, 1.0};
Sebastian Redlce7cd262011-05-20 21:07:01 +0000137 }
138
139 struct B {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000140 B(C, int, C);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000141 };
142
143 void nested_init() {
144 B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
145 }
146}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000147
148namespace 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 Redldbef1bb2011-06-05 12:23:16 +0000172 B(std::initializer_list<int>, T ...);
Sebastian Redl262b62b2011-06-05 12:23:08 +0000173 };
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 Redldc998b42011-07-14 19:08:01 +0000183
184namespace 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}