Clang Project

clang_source_code/test/CXX/drs/dr14xx.cpp
1// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3// RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4// RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5
6#if __cplusplus < 201103L
7// expected-no-diagnostics
8#endif
9
10// dr1425: na abi
11
12namespace dr1460 { // dr1460: 3.5
13#if __cplusplus >= 201103L
14  namespace DRExample {
15    union A {
16      union {};
17      union {};
18      constexpr A() {}
19    };
20    constexpr A a = A();
21
22    union B {
23      union {};
24      union {};
25      constexpr B() = default;
26    };
27    constexpr B b = B();
28
29    union C {
30      union {};
31      union {};
32    };
33    constexpr C c = C();
34#if __cplusplus > 201103L
35    constexpr void f() { C c; }
36    static_assert((f(), true), "");
37#endif
38  }
39
40  union A {};
41  union B { int n; }; // expected-note +{{here}}
42  union C { int n = 0; };
43  struct D { union {}; };
44  struct E { union { int n; }; }; // expected-note +{{here}}
45  struct F { union { int n = 0; }; };
46
47  struct X {
48    friend constexpr A::A() noexcept;
49    friend constexpr B::B() noexcept; // expected-error {{follows non-constexpr declaration}}
50    friend constexpr C::C() noexcept;
51    friend constexpr D::D() noexcept;
52    friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}}
53    friend constexpr F::F() noexcept;
54  };
55
56  // These are OK, because value-initialization doesn't actually invoke the
57  // constructor.
58  constexpr A a = A();
59  constexpr B b = B();
60  constexpr C c = C();
61  constexpr D d = D();
62  constexpr E e = E();
63  constexpr F f = F();
64
65  namespace Defaulted {
66    union A { constexpr A() = default; };
67    union B { int n; constexpr B() = default; }; // expected-error {{not constexpr}}
68    union C { int n = 0; constexpr C() = default; };
69    struct D { union {}; constexpr D() = default; };
70    struct E { union { int n; }; constexpr E() = default; }; // expected-error {{not constexpr}}
71    struct F { union { int n = 0; }; constexpr F() = default; };
72
73    struct G { union { int n = 0; }; union { int m; }; constexpr G() = default; }; // expected-error {{not constexpr}}
74    struct H {
75      union {
76        int n = 0;
77      };
78      union { // expected-note 2{{member not initialized}}
79        int m;
80      };
81      constexpr H() {} // expected-error {{must initialize all members}}
82      constexpr H(bool) : m(1) {}
83      constexpr H(char) : n(1) {} // expected-error {{must initialize all members}}
84      constexpr H(double) : m(1), n(1) {}
85    };
86  }
87
88#if __cplusplus > 201103L
89  template<typename T> constexpr bool check() {
90    T t; // expected-note-re 2{{non-constexpr constructor '{{[BE]}}'}}
91    return true;
92  }
93  static_assert(check<A>(), "");
94  static_assert(check<B>(), ""); // expected-error {{constant}} expected-note {{in call}}
95  static_assert(check<C>(), "");
96  static_assert(check<D>(), "");
97  static_assert(check<E>(), ""); // expected-error {{constant}} expected-note {{in call}}
98  static_assert(check<F>(), "");
99#endif
100
101  union G {
102    int a = 0; // expected-note {{previous initialization is here}}
103    int b = 0; // expected-error {{initializing multiple members of union}}
104  };
105  union H {
106    union {
107      int a = 0; // expected-note {{previous initialization is here}}
108    };
109    union {
110      int b = 0; // expected-error {{initializing multiple members of union}}
111    };
112  };
113  struct I {
114    union {
115      int a = 0; // expected-note {{previous initialization is here}}
116      int b = 0; // expected-error {{initializing multiple members of union}}
117    };
118  };
119  struct J {
120    union { int a = 0; };
121    union { int b = 0; };
122  };
123
124  namespace Overriding {
125    struct A {
126      int a = 1, b, c = 3;
127      constexpr A() : b(2) {}
128    };
129    static_assert(A().a == 1 && A().b == 2 && A().c == 3, "");
130
131    union B {
132      int a, b = 2, c;
133      constexpr B() : a(1) {}
134      constexpr B(char) : b(4) {}
135      constexpr B(int) : c(3) {}
136      constexpr B(const char*) {}
137    };
138    static_assert(B().a == 1, "");
139    static_assert(B().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
140    static_assert(B('x').a == 0, ""); // expected-error {{constant}} expected-note {{read of}}
141    static_assert(B('x').b == 4, "");
142    static_assert(B(123).b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
143    static_assert(B(123).c == 3, "");
144    static_assert(B("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
145    static_assert(B("").b == 2, "");
146    static_assert(B("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
147
148    struct C {
149      union { int a, b = 2, c; };
150      union { int d, e = 5, f; };
151      constexpr C() : a(1) {}
152      constexpr C(char) : c(3) {}
153      constexpr C(int) : d(4) {}
154      constexpr C(float) : f(6) {}
155      constexpr C(const char*) {}
156    };
157
158    static_assert(C().a == 1, "");
159    static_assert(C().b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
160    static_assert(C().d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
161    static_assert(C().e == 5, "");
162
163    static_assert(C('x').b == 2, ""); // expected-error {{constant}} expected-note {{read of}}
164    static_assert(C('x').c == 3, "");
165    static_assert(C('x').d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
166    static_assert(C('x').e == 5, "");
167
168    static_assert(C(1).b == 2, "");
169    static_assert(C(1).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
170    static_assert(C(1).d == 4, "");
171    static_assert(C(1).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
172
173    static_assert(C(1.f).b == 2, "");
174    static_assert(C(1.f).c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
175    static_assert(C(1.f).e == 5, ""); // expected-error {{constant}} expected-note {{read of}}
176    static_assert(C(1.f).f == 6, "");
177
178    static_assert(C("").a == 1, ""); // expected-error {{constant}} expected-note {{read of}}
179    static_assert(C("").b == 2, "");
180    static_assert(C("").c == 3, ""); // expected-error {{constant}} expected-note {{read of}}
181    static_assert(C("").d == 4, ""); // expected-error {{constant}} expected-note {{read of}}
182    static_assert(C("").e == 5, "");
183    static_assert(C("").f == 6, ""); // expected-error {{constant}} expected-note {{read of}}
184
185    struct D;
186    extern const D d;
187    struct D {
188      int a;
189      union {
190        int b = const_cast<D&>(d).a = 1; // not evaluated
191        int c;
192      };
193      constexpr D() : a(0), c(0) {}
194    };
195    constexpr D d {};
196    static_assert(d.a == 0, "");
197  }
198#endif
199}
200
201#if __cplusplus >= 201103L
202namespace std {
203  typedef decltype(sizeof(int)) size_t;
204
205  // libc++'s implementation
206  template <class _E>
207  class initializer_list
208  {
209    const _E* __begin_;
210    size_t    __size_;
211
212    initializer_list(const _E* __b, size_t __s)
213    : __begin_(__b), __size_(__s) {}
214
215  public:
216    typedef _E        value_type;
217    typedef const _E& reference;
218    typedef const _E& const_reference;
219    typedef size_t    size_type;
220
221    typedef const _E* iterator;
222    typedef const _E* const_iterator;
223
224    initializer_list() : __begin_(nullptr), __size_(0) {}
225
226    size_t    size()  const {return __size_;}
227    const _E* begin() const {return __begin_;}
228    const _E* end()   const {return __begin_ + __size_;}
229  };
230} // std
231
232namespace dr1467 {  // dr1467: 3.7 c++11
233  // List-initialization of aggregate from same-type object
234
235  namespace basic0 {
236    struct S {
237      int i = 42;
238    };
239
240    S a;
241    S b(a);
242    S c{a};
243
244    struct SS : public S { } x;
245    S y(x);
246    S z{x};
247  } // basic0
248
249  namespace basic1 {
250    struct S {
251      int i{42};
252    };
253
254    S a;
255    S b(a);
256    S c{a};
257
258    struct SS : public S { } x;
259    S y(x);
260    S z{x};
261  } // basic1
262
263  namespace basic2 {
264    struct S {
265      int i = {42};
266    };
267
268    S a;
269    S b(a);
270    S c{a};
271
272    struct SS : public S { } x;
273    S y(x);
274    S z{x};
275  } // basic2
276
277  namespace dr_example {
278    struct OK {
279      OK() = default;
280      OK(const OK&) = default;
281      OK(int) { }
282    };
283
284    OK ok;
285    OK ok2{ok};
286
287    struct X {
288      X() = default;
289      X(const X&) = default;
290    };
291
292    X x;
293    X x2{x};
294  } // dr_example
295
296  namespace nonaggregate {
297    struct NonAggregate {
298      NonAggregate() {}
299    };
300
301    struct WantsIt {
302      WantsIt(NonAggregate);
303    };
304
305    void f(NonAggregate);
306    void f(WantsIt);
307
308    void test1() {
309      NonAggregate n;
310      f({n});
311    }
312
313    void test2() {
314      NonAggregate x;
315      NonAggregate y{x};
316      NonAggregate z{{x}};
317    }
318  } // nonaggregate
319
320  namespace SelfInitIsNotListInit {
321    struct S {
322      S();
323      explicit S(S &);
324      S(const S &);
325    };
326    S s1;
327    S s2 = {s1}; // ok, not list-initialization so we pick the non-explicit constructor
328  }
329
330  struct NestedInit { int a, b, c; };
331  NestedInit ni[1] = {{NestedInit{1, 2, 3}}};
332
333  namespace NestedInit2 {
334    struct Pair { int a, b; };
335    struct TwoPairs { TwoPairs(Pair, Pair); };
336    struct Value { Value(Pair); Value(TwoPairs); };
337    void f() { Value{{{1,2},{3,4}}}; }
338  }
339} // dr1467
340
341namespace dr1490 {  // dr1490: 3.7 c++11
342  // List-initialization from a string literal
343
344  char s[4]{"abc"};                   // Ok
345  std::initializer_list<char>{"abc"}; // expected-error {{expected unqualified-id}}}
346} // dr190
347
348namespace dr1495 { // dr1495: 4
349  // Deduction succeeds in both directions.
350  template<typename T, typename U> struct A {}; // expected-note {{template is declared here}}
351  template<typename T, typename U> struct A<U, T> {}; // expected-error {{class template partial specialization is not more specialized}}
352
353  // Primary template is more specialized.
354  template<typename, typename...> struct B {}; // expected-note {{template is declared here}}
355  template<typename ...Ts> struct B<Ts...> {}; // expected-error {{not more specialized}}
356
357  // Deduction fails in both directions.
358  template<int, typename, typename ...> struct C {}; // expected-note {{template is declared here}}
359  template<typename ...Ts> struct C<0, Ts...> {}; // expected-error {{not more specialized}}
360
361#if __cplusplus >= 201402L
362  // Deduction succeeds in both directions.
363  template<typename T, typename U> int a; // expected-note {{template is declared here}}
364  template<typename T, typename U> int a<U, T>; // expected-error {{variable template partial specialization is not more specialized}}
365
366  // Primary template is more specialized.
367  template<typename, typename...> int b; // expected-note {{template is declared here}}
368  template<typename ...Ts> int b<Ts...>; // expected-error {{not more specialized}}
369
370  // Deduction fails in both directions.
371  template<int, typename, typename ...> int c; // expected-note {{template is declared here}}
372  template<typename ...Ts> int c<0, Ts...>; // expected-error {{not more specialized}}
373#endif
374}
375#endif
376