Clang Project

clang_source_code/test/CXX/dcl.decl/dcl.init/dcl.init.list/p3.cpp
1// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
3// FIXME: Remove the triple when PR27098 is fixed.
4// RUN: %clang_cc1 -std=c++1z -fsyntax-only -verify %s -triple %itanium_abi_triple
5
6namespace std {
7  typedef decltype(sizeof(int)) size_t;
8
9  template <typename E>
10  struct initializer_list
11  {
12    const E *p;
13    size_t n;
14    initializer_list(const E *p, size_t n) : p(p), n(n) {}
15  };
16
17  struct string {
18    string(const char *);
19  };
20
21  template<typename A, typename B>
22  struct pair {
23    pair(const A&, const B&);
24  };
25}
26
27namespace bullet1 {
28  double ad[] = { 1, 2.0 };
29  int ai[] = { 1, 2.0 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
30
31  struct S2 {
32    int m1;
33    double m2, m3;
34  };
35
36  S2 s21 = { 1, 2, 3.0 };
37  S2 s22 { 1.0, 2, 3 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
38  S2 s23 { };
39}
40
41namespace bullet4_example1 {
42  struct S {
43    S(std::initializer_list<double> d) {}
44    S(std::initializer_list<int> i) {}
45    S() {}
46  };
47
48  S s1 = { 1.0, 2.0, 3.0 };
49  S s2 = { 1, 2, 3 };
50  S s3 = { };
51}
52
53namespace bullet4_example2 {
54  struct Map {
55    Map(std::initializer_list<std::pair<std::string,int>>) {}
56  };
57
58  Map ship = {{"Sophie",14}, {"Surprise",28}};
59}
60
61namespace bullet4_example3 {
62  struct S {
63    S(int, double, double) {}
64    S() {}
65  };
66
67  S s1 = { 1, 2, 3.0 };
68  S s2 { 1.0, 2, 3 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
69  S s3 {};
70}
71
72namespace bullet5 {
73  int x1 {2};
74  int x2 {2.0};  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
75}
76
77namespace bullet6 {
78  struct S {
79    S(std::initializer_list<double>) {}
80    S(const std::string &) {}
81  };
82
83  const S& r1 = { 1, 2, 3.0 };
84  const S& r2 = { "Spinach" };
85  S& r3 = { 1, 2, 3 };  // expected-error {{non-const lvalue reference to type 'bullet6::S' cannot bind to an initializer list temporary}}
86  const int& i1 = { 1 };
87  const int& i2 = { 1.1 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
88  const int (&iar)[2] = { 1, 2 };
89}
90
91namespace bullet7 {
92  int** pp {};
93}
94
95namespace bullet8 {
96  struct A { int i; int j; };
97  A a1 { 1, 2 };
98  A a2 { 1.2 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
99
100  struct B {
101    B(std::initializer_list<int> i) {}
102  };
103  B b1 { 1, 2 };
104  B b2 { 1, 2.0 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
105
106  struct C {
107    C(int i, double j) {}
108  };
109  C c1 = { 1, 2.2 };
110  // FIXME: Suppress the narrowing warning in the cases where we issue a narrowing error.
111  C c2 = { 1.1, 2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
112
113  int j { 1 };
114  int k { };
115}
116
117namespace rdar13395022 {
118  struct MoveOnly { // expected-note {{candidate}}
119    MoveOnly(MoveOnly&&); // expected-note 2{{copy constructor is implicitly deleted because}} expected-note {{candidate}}
120  };
121
122  void test(MoveOnly mo) {
123    auto &&list1 = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'std::initializer_list}}
124    MoveOnly (&&list2)[1] = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]'}}
125    std::initializer_list<MoveOnly> &&list3 = {};
126    MoveOnly (&&list4)[1] = {}; // expected-error {{no matching constructor}}
127    // expected-note@-1 {{in implicit initialization of array element 0 with omitted initializer}}
128    // expected-note@-2 {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]' created to list-initialize this reference}}
129  }
130}
131
132namespace cxx1z_direct_enum_init {
133  enum A {};
134  enum B : char {};
135  enum class C {};
136  enum class D : char {};
137  enum class E : char { k = 5 };
138
139  template<typename T> void good() {
140    (void)T{0};
141    T t1{0};
142    T t2 = T{0};
143
144    struct S { T t; };
145    S s{T{0}};
146
147    struct U { T t{0}; } u; // expected-note 0+{{instantiation of}}
148
149    struct V { T t; V() : t{0} {} }; // expected-note 0+{{instantiation of}}
150
151    void f(T);
152    f(T{0});
153
154    char c;
155    auto t3 = T{c};
156  }
157#if __cplusplus <= 201402L
158  // expected-error@-18 5{{cannot initialize}}
159  // expected-error@-18 5{{cannot initialize}}
160  // expected-error@-18 5{{cannot initialize}}
161  //
162  //
163  // expected-error@-18 5{{cannot initialize}}
164  //
165  // expected-error@-18 5{{cannot initialize}}
166  //
167  // expected-error@-18 5{{cannot initialize}}
168  //
169  //
170  // expected-error@-18 5{{cannot initialize}}
171  //
172  //
173  // expected-error@-18 5{{cannot initialize}}
174#else
175  // expected-error@-35 {{cannot initialize}}
176  // expected-error@-35 {{cannot initialize}}
177  // expected-error@-35 {{cannot initialize}}
178  //
179  //
180  // expected-error@-35 {{cannot initialize}}
181  //
182  // expected-error@-35 {{cannot initialize}}
183  //
184  // expected-error@-35 {{cannot initialize}}
185  //
186  //
187  // expected-error@-35 {{cannot initialize}}
188  //
189  //
190  // expected-error@-35 {{cannot initialize}}
191#endif
192
193  template<typename T> void bad() {
194    T t = {0};
195
196    struct S { T t; };
197    S s1{0};
198    S s2{{0}};
199
200    struct U { T t = {0}; } u; // expected-note 0+{{instantiation of}}
201
202    struct V { T t; V() : t({0}) {} }; // expected-note 0+{{instantiation of}}
203
204    void f(T); // expected-note 0+{{passing argument}}
205    f({0});
206  }
207  // expected-error@-13 5{{cannot initialize}}
208  //
209  //
210  // expected-error@-13 5{{cannot initialize}}
211  // expected-error@-13 5{{cannot initialize}}
212  //
213  // expected-error@-13 5{{cannot initialize}}
214  //
215  // expected-error@-13 5{{cannot initialize}}
216  //
217  //
218  // expected-error@-13 5{{cannot initialize}}
219
220  template<typename T> void ugly() {
221    extern char c;
222    T t1{char('0' + c)};
223    T t2{'0' + c};
224    T t3{1234};
225  }
226#if __cplusplus <= 201402L
227  // expected-error@-5 4{{cannot initialize}}
228  // expected-error@-5 4{{cannot initialize}}
229  // expected-error@-5 4{{cannot initialize}}
230#else
231  // expected-error@-8 3{{non-constant-expression cannot be narrowed}}
232  // expected-error@-8 3{{constant expression evaluates to 1234 which cannot be narrowed}} expected-warning@-8 {{changes value}}
233#endif
234
235  void test() {
236    good<A>(); // expected-note 4{{instantiation of}}
237    good<B>();
238    good<C>();
239    good<D>();
240    good<E>();
241#if __cplusplus <= 201402L
242    // expected-note@-5 4{{instantiation of}}
243    // expected-note@-5 4{{instantiation of}}
244    // expected-note@-5 4{{instantiation of}}
245    // expected-note@-5 4{{instantiation of}}
246#endif
247
248    bad<A>(); // expected-note 4{{instantiation of}}
249    bad<B>(); // expected-note 4{{instantiation of}}
250    bad<C>(); // expected-note 4{{instantiation of}}
251    bad<D>(); // expected-note 4{{instantiation of}}
252    bad<E>(); // expected-note 4{{instantiation of}}
253
254    ugly<B>(); // expected-note {{instantiation of}}
255    ugly<C>(); // ok
256    ugly<D>(); // expected-note {{instantiation of}}
257    ugly<E>(); // expected-note {{instantiation of}}
258#if __cplusplus <= 201402L
259    // expected-note@-4 {{instantiation of}}
260#else
261    (void)B{0.0}; // expected-error {{type 'double' cannot be narrowed}}
262#endif
263  }
264
265#if __cplusplus > 201402L
266  enum class F : unsigned {};
267  F f1(unsigned x) { return F{x}; }
268  F f2(const unsigned x) { return F{x}; }
269  F f3(bool x) { return F{x}; }
270  F f4(const bool x) { return F{x}; }
271#endif
272}
273