Welcome to mirror list, hosted at ThFree Co, Russian Federation.

p3.cpp « dcl.init.list « dcl.init « dcl.decl « CXX « test « clang - github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0100d78ca844d419a92c7d44b677c2acb4a0c5ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
// RUN: %clang_cc1 -std=c++1z -fsyntax-only -verify %s

namespace std {
  typedef decltype(sizeof(int)) size_t;

  template <typename E>
  struct initializer_list
  {
    const E *p;
    size_t n;
    initializer_list(const E *p, size_t n) : p(p), n(n) {}
  };

  struct string {
    string(const char *);
  };

  template<typename A, typename B>
  struct pair {
    pair(const A&, const B&);
  };
}

namespace bullet1 {
  double ad[] = { 1, 2.0 };
  int ai[] = { 1, 2.0 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}

  struct S2 {
    int m1;
    double m2, m3;
  };

  S2 s21 = { 1, 2, 3.0 };
  S2 s22 { 1.0, 2, 3 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
  S2 s23 { };
}

namespace bullet4_example1 {
  struct S {
    S(std::initializer_list<double> d) {}
    S(std::initializer_list<int> i) {}
    S() {}
  };

  S s1 = { 1.0, 2.0, 3.0 };
  S s2 = { 1, 2, 3 };
  S s3 = { };
}

namespace bullet4_example2 {
  struct Map {
    Map(std::initializer_list<std::pair<std::string,int>>) {}
  };

  Map ship = {{"Sophie",14}, {"Surprise",28}};
}

namespace bullet4_example3 {
  struct S {
    S(int, double, double) {}
    S() {}
  };

  S s1 = { 1, 2, 3.0 };
  S s2 { 1.0, 2, 3 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
  S s3 {};
}

namespace bullet5 {
  int x1 {2};
  int x2 {2.0};  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
}

namespace bullet6 {
  struct S {
    S(std::initializer_list<double>) {}
    S(const std::string &) {}
  };

  const S& r1 = { 1, 2, 3.0 };
  const S& r2 = { "Spinach" };
  S& r3 = { 1, 2, 3 };  // expected-error {{non-const lvalue reference to type 'S' cannot bind to an initializer list temporary}}
  const int& i1 = { 1 };
  const int& i2 = { 1.1 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
  const int (&iar)[2] = { 1, 2 };

  // We interpret "class type with a default constructor" as including the case
  // where a default constructor is inherited.
  struct X {
    X();
    X(std::initializer_list<int>) = delete;
  };
  struct Y : X {
    using X::X;
    Y(int);
  };
  Y y1{};
  void use() { Y y; }
  Y y2{};
}

namespace bullet7 {
  int** pp {};
}

namespace bullet8 {
  struct A { int i; int j; };
  A a1 { 1, 2 };
  A a2 { 1.2 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}

  struct B {
    B(std::initializer_list<int> i) {}
  };
  B b1 { 1, 2 };
  B b2 { 1, 2.0 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}

  struct C {
    C(int i, double j) {}
  };
  C c1 = { 1, 2.2 };
  // FIXME: Suppress the narrowing warning in the cases where we issue a narrowing error.
  C c2 = { 1.1, 2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}

  int j { 1 };
  int k { };
}

namespace rdar13395022 {
  struct MoveOnly { // expected-note {{candidate}}
    MoveOnly(MoveOnly&&); // expected-note 2{{copy constructor is implicitly deleted because}} expected-note {{candidate}}
  };

  void test(MoveOnly mo) {
    auto &&list1 = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'std::initializer_list}}
    MoveOnly (&&list2)[1] = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'MoveOnly[1]'}}
    std::initializer_list<MoveOnly> &&list3 = {};
    MoveOnly (&&list4)[1] = {}; // expected-error {{no matching constructor}}
    // expected-note@-1 {{in implicit initialization of array element 0 with omitted initializer}}
    // expected-note@-2 {{in initialization of temporary of type 'MoveOnly[1]' created to list-initialize this reference}}
  }
}

namespace cxx1z_direct_enum_init {
  enum A {};
  enum B : char {};
  enum class C {};
  enum class D : char {};
  enum class E : char { k = 5 };

  template<typename T> void good() {
    (void)T{0};
    T t1{0};
    T t2 = T{0};

    struct S { T t; };
    S s{T{0}};

    struct U { T t{0}; } u; // expected-note 0+{{instantiation of}}

    struct V { T t; V() : t{0} {} }; // expected-note 0+{{instantiation of}}

    void f(T);
    f(T{0});

    char c;
    auto t3 = T{c};
  }
#if __cplusplus <= 201402L
  // expected-error@-18 5{{cannot initialize}}
  // expected-error@-18 5{{cannot initialize}}
  // expected-error@-18 5{{cannot initialize}}
  //
  //
  // expected-error@-18 5{{cannot initialize}}
  //
  // expected-error@-18 5{{cannot initialize}}
  //
  // expected-error@-18 5{{cannot initialize}}
  //
  //
  // expected-error@-18 5{{cannot initialize}}
  //
  //
  // expected-error@-18 5{{cannot initialize}}
#else
  // expected-error@-35 {{cannot initialize}}
  // expected-error@-35 {{cannot initialize}}
  // expected-error@-35 {{cannot initialize}}
  //
  //
  // expected-error@-35 {{cannot initialize}}
  //
  // expected-error@-35 {{cannot initialize}}
  //
  // expected-error@-35 {{cannot initialize}}
  //
  //
  // expected-error@-35 {{cannot initialize}}
  //
  //
  // expected-error@-35 {{cannot initialize}}
#endif

  template<typename T> void bad() {
    T t = {0};

    struct S { T t; };
    S s1{0};
    S s2{{0}};

    struct U { T t = {0}; } u; // expected-note 0+{{instantiation of}}

    struct V { T t; V() : t({0}) {} }; // expected-note 0+{{instantiation of}}

    void f(T); // expected-note 0+{{passing argument}}
    f({0});
  }
  // expected-error@-13 5{{cannot initialize}}
  //
  //
  // expected-error@-13 5{{cannot initialize}}
  // expected-error@-13 5{{cannot initialize}}
  //
  // expected-error@-13 5{{cannot initialize}}
  //
  // expected-error@-13 5{{cannot initialize}}
  //
  //
  // expected-error@-13 5{{cannot initialize}}

  template<typename T> void ugly() {
    extern char c;
    T t1{char('0' + c)};
    T t2{'0' + c};
    T t3{1234};
  }
#if __cplusplus <= 201402L
  // expected-error@-5 4{{cannot initialize}}
  // expected-error@-5 4{{cannot initialize}}
  // expected-error@-5 4{{cannot initialize}}
#else
  // expected-error@-8 3{{non-constant-expression cannot be narrowed}}
  // expected-error@-8 3{{constant expression evaluates to 1234 which cannot be narrowed}} expected-warning@-8 {{changes value}}
#endif

  void test() {
    good<A>(); // expected-note 4{{instantiation of}}
    good<B>();
    good<C>();
    good<D>();
    good<E>();
#if __cplusplus <= 201402L
    // expected-note@-5 4{{instantiation of}}
    // expected-note@-5 4{{instantiation of}}
    // expected-note@-5 4{{instantiation of}}
    // expected-note@-5 4{{instantiation of}}
#endif

    bad<A>(); // expected-note 4{{instantiation of}}
    bad<B>(); // expected-note 4{{instantiation of}}
    bad<C>(); // expected-note 4{{instantiation of}}
    bad<D>(); // expected-note 4{{instantiation of}}
    bad<E>(); // expected-note 4{{instantiation of}}

    ugly<B>(); // expected-note {{instantiation of}}
    ugly<C>(); // ok
    ugly<D>(); // expected-note {{instantiation of}}
    ugly<E>(); // expected-note {{instantiation of}}
#if __cplusplus <= 201402L
    // expected-note@-4 {{instantiation of}}
#else
    (void)B{0.0}; // expected-error {{type 'double' cannot be narrowed}}
#endif
  }

#if __cplusplus > 201402L
  enum class F : unsigned {};
  F f1(unsigned x) { return F{x}; }
  F f2(const unsigned x) { return F{x}; }
  F f3(bool x) { return F{x}; }
  F f4(const bool x) { return F{x}; }
#endif
}