blob: 2f13151a53a6cd1678815d7510f1a2ea998036d7 [file] [log] [blame]
Sebastian Redlce7cd262011-05-20 21:07:01 +00001// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s
2// 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
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, ""); }
48 { const int a{1, 2}; } // expected-error {{ too many initializers}}
49 { const int a = {1, 2}; } // expected-error {{ too many initializers}}
50 { const short a = {100000}; } // expected-error {{narrowing conversion}}
51 }
52
53 int function_call() {
54 void takes_int(int);
55 takes_int({1});
56
57 int ar[10];
58 (void) ar[{1}]; // expected-error {{initializer list is illegal with the built-in index operator}}
59
60 return {1};
61 }
62
63 void inline_init() {
64 (void) int{1};
65 (void) new int{1};
66 }
67
68 void initializer_list() {
69 auto l = {1, 2, 3, 4};
70 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
71
72 for (int i : {1, 2, 3, 4}) {}
73 }
74
75 struct A {
76 int i;
77 A() : i{1} {}
78 };
79
80}
81
82namespace objects {
83
Sebastian Redl262b62b2011-06-05 12:23:08 +000084 template <int N>
Sebastian Redlce7cd262011-05-20 21:07:01 +000085 struct A {
Sebastian Redl262b62b2011-06-05 12:23:08 +000086 A() { static_assert(N == 0, ""); }
87 A(int, double) { static_assert(N == 1, ""); }
88 A(int, int) { static_assert(N == 2, ""); }
89 A(std::initializer_list<int>) { static_assert(N == 3, ""); }
Sebastian Redlce7cd262011-05-20 21:07:01 +000090 };
91
92 void initialization() {
93 // FIXME: how to ensure correct overloads are called?
Sebastian Redl262b62b2011-06-05 12:23:08 +000094 { A<0> a{}; }
95 { A<0> a = {}; }
96 { A<1> a{1, 1.0}; }
97 { A<1> a = {1, 1.0}; }
98 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
99 { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; }
100 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
101 { A<3> a{1, 2}; }
Sebastian Redlce7cd262011-05-20 21:07:01 +0000102 }
103
Sebastian Redl262b62b2011-06-05 12:23:08 +0000104 struct C {
105 C();
106 C(int, double);
107 C(int, int);
108 C(std::initializer_list<int>);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000109
Sebastian Redl262b62b2011-06-05 12:23:08 +0000110 int operator[](C);
111 };
112
113 C function_call() {
114 void takes_C(C);
115 takes_C({1, 1.0});
116
117 C c;
118 c[{1, 1.0}];
Sebastian Redlce7cd262011-05-20 21:07:01 +0000119
120 return {1, 1.0};
121 }
122
123 void inline_init() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000124 (void) A<1>{1, 1.0};
125 (void) new A<1>{1, 1.0};
Sebastian Redlce7cd262011-05-20 21:07:01 +0000126 }
127
128 struct B {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000129 B(C, int, C);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000130 };
131
132 void nested_init() {
133 B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
134 }
135}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000136
137namespace litb {
138
139 // invalid
140 struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}}
141
142 // invalid
143 int a({0}); // expected-error {{}}
144
145 // invalid
146 int const &b({0}); // expected-error {{}}
147
148 struct C { explicit C(int, int); C(int, long); };
149
150 // invalid
151 C c({1, 2}); // expected-error {{}}
152
153 // valid (by copy constructor).
154 C d({1, 2L}); // expected-error {{}}
155
156 // valid
157 C e{1, 2};
158
159 struct B {
160 template<typename ...T>
161 B(initializer_list<int>, T ...);
162 };
163
164 // invalid (the first phase only considers init-list ctors)
165 // (for the second phase, no constructor is viable)
166 B f{1, 2, 3};
167
168 // valid (T deduced to <>).
169 B g({1, 2, 3});
170
171}