blob: fc903f0016c26010feddeeac88d504d7121358cf [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, ""); }
Sebastian Redldbef1bb2011-06-05 12:23:16 +000048 { 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 Redlce7cd262011-05-20 21:07:01 +000051 { 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() {
70 auto l = {1, 2, 3, 4};
71 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
72
73 for (int i : {1, 2, 3, 4}) {}
74 }
75
76 struct A {
77 int i;
78 A() : i{1} {}
79 };
80
81}
82
83namespace objects {
84
Sebastian Redl262b62b2011-06-05 12:23:08 +000085 template <int N>
Sebastian Redlce7cd262011-05-20 21:07:01 +000086 struct A {
Sebastian Redl262b62b2011-06-05 12:23:08 +000087 A() { static_assert(N == 0, ""); }
88 A(int, double) { static_assert(N == 1, ""); }
89 A(int, int) { static_assert(N == 2, ""); }
90 A(std::initializer_list<int>) { static_assert(N == 3, ""); }
Sebastian Redlce7cd262011-05-20 21:07:01 +000091 };
92
93 void initialization() {
94 // FIXME: how to ensure correct overloads are called?
Sebastian Redl262b62b2011-06-05 12:23:08 +000095 { A<0> a{}; }
96 { A<0> a = {}; }
97 { A<1> a{1, 1.0}; }
98 { A<1> a = {1, 1.0}; }
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, 3, 4, 5, 6, 7, 8}; }
102 { A<3> a{1, 2}; }
Sebastian Redlce7cd262011-05-20 21:07:01 +0000103 }
104
Sebastian Redl262b62b2011-06-05 12:23:08 +0000105 struct C {
106 C();
107 C(int, double);
108 C(int, int);
109 C(std::initializer_list<int>);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000110
Sebastian Redl262b62b2011-06-05 12:23:08 +0000111 int operator[](C);
112 };
113
114 C function_call() {
115 void takes_C(C);
116 takes_C({1, 1.0});
117
118 C c;
119 c[{1, 1.0}];
Sebastian Redlce7cd262011-05-20 21:07:01 +0000120
121 return {1, 1.0};
122 }
123
124 void inline_init() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000125 (void) A<1>{1, 1.0};
126 (void) new A<1>{1, 1.0};
Sebastian Redlce7cd262011-05-20 21:07:01 +0000127 }
128
129 struct B {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000130 B(C, int, C);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000131 };
132
133 void nested_init() {
134 B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
135 }
136}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000137
138namespace litb {
139
140 // invalid
141 struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}}
142
143 // invalid
144 int a({0}); // expected-error {{}}
145
146 // invalid
147 int const &b({0}); // expected-error {{}}
148
149 struct C { explicit C(int, int); C(int, long); };
150
151 // invalid
152 C c({1, 2}); // expected-error {{}}
153
154 // valid (by copy constructor).
155 C d({1, 2L}); // expected-error {{}}
156
157 // valid
158 C e{1, 2};
159
160 struct B {
161 template<typename ...T>
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000162 B(std::initializer_list<int>, T ...);
Sebastian Redl262b62b2011-06-05 12:23:08 +0000163 };
164
165 // invalid (the first phase only considers init-list ctors)
166 // (for the second phase, no constructor is viable)
167 B f{1, 2, 3};
168
169 // valid (T deduced to <>).
170 B g({1, 2, 3});
171
172}