blob: ec37a0c9ac18173f106b0c4ea62015e650b3567d [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() {
Sebastian Redl064e2362011-06-05 12:23:21 +000070 std::initializer_list<int> il = { 1, 2, 3 };
71 std::initializer_list<double> dl = { 1.0, 2.0, 3 };
Sebastian Redlce7cd262011-05-20 21:07:01 +000072 auto l = {1, 2, 3, 4};
73 static_assert(same_type<decltype(l), std::initializer_list<int>>::value, "");
Sebastian Redl064e2362011-06-05 12:23:21 +000074 auto bl = {1, 2.0}; // expected-error {{cannot deduce}}
Sebastian Redlce7cd262011-05-20 21:07:01 +000075
76 for (int i : {1, 2, 3, 4}) {}
77 }
78
79 struct A {
80 int i;
81 A() : i{1} {}
82 };
83
84}
85
86namespace objects {
87
Sebastian Redl262b62b2011-06-05 12:23:08 +000088 template <int N>
Sebastian Redlce7cd262011-05-20 21:07:01 +000089 struct A {
Sebastian Redl262b62b2011-06-05 12:23:08 +000090 A() { static_assert(N == 0, ""); }
91 A(int, double) { static_assert(N == 1, ""); }
92 A(int, int) { static_assert(N == 2, ""); }
93 A(std::initializer_list<int>) { static_assert(N == 3, ""); }
Sebastian Redlce7cd262011-05-20 21:07:01 +000094 };
95
96 void initialization() {
Sebastian Redl262b62b2011-06-05 12:23:08 +000097 { A<0> a{}; }
98 { A<0> a = {}; }
99 { A<1> a{1, 1.0}; }
100 { A<1> a = {1, 1.0}; }
101 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
102 { A<3> a = {1, 2, 3, 4, 5, 6, 7, 8}; }
103 { A<3> a{1, 2, 3, 4, 5, 6, 7, 8}; }
104 { A<3> a{1, 2}; }
Sebastian Redlce7cd262011-05-20 21:07:01 +0000105 }
106
Sebastian Redl262b62b2011-06-05 12:23:08 +0000107 struct C {
108 C();
109 C(int, double);
110 C(int, int);
111 C(std::initializer_list<int>);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000112
Sebastian Redl262b62b2011-06-05 12:23:08 +0000113 int operator[](C);
114 };
115
116 C function_call() {
117 void takes_C(C);
118 takes_C({1, 1.0});
119
120 C c;
121 c[{1, 1.0}];
Sebastian Redlce7cd262011-05-20 21:07:01 +0000122
123 return {1, 1.0};
124 }
125
126 void inline_init() {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000127 (void) A<1>{1, 1.0};
128 (void) new A<1>{1, 1.0};
Sebastian Redlce7cd262011-05-20 21:07:01 +0000129 }
130
131 struct B {
Sebastian Redl262b62b2011-06-05 12:23:08 +0000132 B(C, int, C);
Sebastian Redlce7cd262011-05-20 21:07:01 +0000133 };
134
135 void nested_init() {
136 B b{{1, 1.0}, 2, {3, 4, 5, 6, 7}};
137 }
138}
Sebastian Redl262b62b2011-06-05 12:23:08 +0000139
140namespace litb {
141
142 // invalid
143 struct A { int a[2]; A():a({1, 2}) { } }; // expected-error {{}}
144
145 // invalid
146 int a({0}); // expected-error {{}}
147
148 // invalid
149 int const &b({0}); // expected-error {{}}
150
151 struct C { explicit C(int, int); C(int, long); };
152
153 // invalid
154 C c({1, 2}); // expected-error {{}}
155
156 // valid (by copy constructor).
157 C d({1, 2L}); // expected-error {{}}
158
159 // valid
160 C e{1, 2};
161
162 struct B {
163 template<typename ...T>
Sebastian Redldbef1bb2011-06-05 12:23:16 +0000164 B(std::initializer_list<int>, T ...);
Sebastian Redl262b62b2011-06-05 12:23:08 +0000165 };
166
167 // invalid (the first phase only considers init-list ctors)
168 // (for the second phase, no constructor is viable)
169 B f{1, 2, 3};
170
171 // valid (T deduced to <>).
172 B g({1, 2, 3});
173
174}