Clang Project

clang_source_code/test/CXX/drs/dr16xx.cpp
1// RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
2// RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
3// RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
4// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
5// RUN: %clang_cc1 -std=c++2a -triple x86_64-unknown-unknown %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
6
7#if __cplusplus < 201103L
8// expected-error@+1 {{variadic macro}}
9#define static_assert(...) __extension__ _Static_assert(__VA_ARGS__)
10#endif
11
12#if __cplusplus >= 201103L
13namespace std {
14  typedef decltype(sizeof(int)) size_t;
15
16  template<typename E> class initializer_list {
17    const E *begin;
18    size_t size;
19
20  public:
21    initializer_list();
22  };
23} // std
24#endif
25
26namespace dr1611 { // dr1611: dup 1658
27  struct A { A(int); };
28  struct B : virtual A { virtual void f() = 0; };
29  struct C : B { C() : A(0) {} void f(); };
30  C c;
31}
32
33namespace dr1684 { // dr1684: 3.6
34#if __cplusplus >= 201103L
35  struct NonLiteral { // expected-note {{because}}
36    NonLiteral();
37    constexpr int f() { return 0; } // expected-warning 0-1{{will not be implicitly 'const'}}
38  };
39  constexpr int f(NonLiteral &) { return 0; }
40  constexpr int f(NonLiteral) { return 0; } // expected-error {{not a literal type}}
41#endif
42}
43
44namespace dr1631 {  // dr1631: 3.7
45#if __cplusplus >= 201103L
46  // Incorrect overload resolution for single-element initializer-list
47
48  struct A { int a[1]; };
49  struct B { B(int); };
50  void f(B, int);
51  void f(B, int, int = 0);
52  void f(int, A);
53
54  void test() {
55    f({0}, {{1}}); // expected-warning {{braces around scalar init}}
56  }
57
58  namespace with_error {
59    void f(B, int);           // TODO: expected- note {{candidate function}}
60    void f(int, A);           // expected-note {{candidate function}}
61    void f(int, A, int = 0);  // expected-note {{candidate function}}
62    
63    void test() {
64      f({0}, {{1}});        // expected-error{{call to 'f' is ambiguous}}
65    }
66  }
67#endif
68}
69
70namespace dr1638 { // dr1638: yes
71#if __cplusplus >= 201103L
72  template<typename T> struct A {
73    enum class E; // expected-note {{previous}}
74    enum class F : T; // expected-note 2{{previous}}
75  };
76
77  template<> enum class A<int>::E;
78  template<> enum class A<int>::E {};
79  template<> enum class A<int>::F : int;
80  template<> enum class A<int>::F : int {};
81
82  template<> enum class A<short>::E : int;
83  template<> enum class A<short>::E : int {};
84
85  template<> enum class A<short>::F; // expected-error {{different underlying type}}
86  template<> enum class A<char>::E : char; // expected-error {{different underlying type}}
87  template<> enum class A<char>::F : int; // expected-error {{different underlying type}}
88
89  enum class A<unsigned>::E; // expected-error {{template specialization requires 'template<>'}} expected-error {{nested name specifier}}
90  template enum class A<unsigned>::E; // expected-error {{enumerations cannot be explicitly instantiated}}
91  enum class A<unsigned>::E *e; // expected-error {{must use 'enum' not 'enum class'}}
92
93  struct B {
94    friend enum class A<unsigned>::E; // expected-error {{must use 'enum' not 'enum class'}}
95  };
96#endif
97}
98
99namespace dr1645 { // dr1645: 3.9
100#if __cplusplus >= 201103L
101  struct A {
102    constexpr A(int, float = 0); // expected-note 2{{candidate}}
103    explicit A(int, int = 0); // expected-note 2{{candidate}}
104    A(int, int, int = 0) = delete; // expected-note {{candidate}}
105  };
106
107  struct B : A { // expected-note 2{{candidate}}
108    using A::A; // expected-note 5{{inherited here}}
109  };
110
111  constexpr B a(0); // expected-error {{ambiguous}}
112  constexpr B b(0, 0); // expected-error {{ambiguous}}
113#endif
114}
115
116namespace dr1653 { // dr1653: 4 c++17
117  void f(bool b) {
118    ++b;
119    b++;
120#if __cplusplus <= 201402L
121    // expected-warning@-3 {{deprecated}} expected-warning@-2 {{deprecated}}
122#else
123    // expected-error@-5 {{incrementing expression of type bool}} expected-error@-4 {{incrementing expression of type bool}}
124#endif
125    --b; // expected-error {{cannot decrement expression of type bool}}
126    b--; // expected-error {{cannot decrement expression of type bool}}
127    b += 1; // ok
128    b -= 1; // ok
129  }
130}
131
132namespace dr1658 { // dr1658: 5
133  namespace DefCtor {
134    class A { A(); }; // expected-note 0-2{{here}}
135    class B { ~B(); }; // expected-note 0-2{{here}}
136
137    // The stars align! An abstract class does not construct its virtual bases.
138    struct C : virtual A { C(); virtual void foo() = 0; };
139    C::C() = default; // ok, not deleted, expected-error 0-1{{extension}}
140    struct D : virtual B { D(); virtual void foo() = 0; };
141    D::D() = default; // ok, not deleted, expected-error 0-1{{extension}}
142
143    // In all other cases, we are not so lucky.
144    struct E : A { E(); virtual void foo() = 0; };
145#if __cplusplus < 201103L
146    E::E() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
147#else
148    E::E() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
149#endif
150    struct F : virtual A { F(); };
151#if __cplusplus < 201103L
152    F::F() = default; // expected-error {{private default constructor}} expected-error {{extension}} expected-note {{here}}
153#else
154    F::F() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible default constructor}}
155#endif
156
157    struct G : B { G(); virtual void foo() = 0; };
158#if __cplusplus < 201103L
159    G::G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
160#else
161    G::G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
162#endif
163    struct H : virtual B { H(); };
164#if __cplusplus < 201103L
165    H::H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
166#else
167    H::H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
168#endif
169  }
170
171  namespace Dtor {
172    class B { ~B(); }; // expected-note 0-2{{here}}
173
174    struct D : virtual B { ~D(); virtual void foo() = 0; };
175    D::~D() = default; // ok, not deleted, expected-error 0-1{{extension}}
176
177    struct G : B { ~G(); virtual void foo() = 0; };
178#if __cplusplus < 201103L
179    G::~G() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
180#else
181    G::~G() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
182#endif
183    struct H : virtual B { ~H(); };
184#if __cplusplus < 201103L
185    H::~H() = default; // expected-error@-2 {{private destructor}} expected-error {{extension}} expected-note {{here}}
186#else
187    H::~H() = default; // expected-error {{would delete}} expected-note@-4{{inaccessible destructor}}
188#endif
189  }
190
191  namespace MemInit {
192    struct A { A(int); }; // expected-note {{here}}
193    struct B : virtual A {
194      B() {}
195      virtual void f() = 0;
196    };
197    struct C : virtual A {
198      C() {} // expected-error {{must explicitly initialize}}
199    };
200  }
201
202  namespace CopyCtorParamType {
203    struct A { A(A&); };
204    struct B : virtual A { virtual void f() = 0; };
205    struct C : virtual A { virtual void f(); };
206    struct D : A { virtual void f() = 0; };
207
208    struct X {
209      friend B::B(const B&) throw();
210      friend C::C(C&);
211      friend D::D(D&);
212    };
213  }
214
215  namespace CopyCtor {
216    class A { A(const A&); A(A&&); }; // expected-note 0-4{{here}} expected-error 0-1{{extension}}
217
218    struct C : virtual A { C(const C&); C(C&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
219    C::C(const C&) = default; // expected-error 0-1{{extension}}
220    C::C(C&&) = default; // expected-error 0-2{{extension}}
221
222    struct E : A { E(const E&); E(E&&); virtual void foo() = 0; }; // expected-error 0-1{{extension}}
223#if __cplusplus < 201103L
224    E::E(const E&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
225    E::E(E&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
226#else
227    E::E(const E&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
228    E::E(E&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
229#endif
230    struct F : virtual A { F(const F&); F(F&&); }; // expected-error 0-1{{extension}}
231#if __cplusplus < 201103L
232    F::F(const F&) = default; // expected-error {{private copy constructor}} expected-error {{extension}} expected-note {{here}}
233    F::F(F&&) = default; // expected-error {{private move constructor}} expected-error 2{{extension}} expected-note {{here}}
234#else
235    F::F(const F&) = default; // expected-error {{would delete}} expected-note@-5{{inaccessible copy constructor}}
236    F::F(F&&) = default; // expected-error {{would delete}} expected-note@-6{{inaccessible move constructor}}
237#endif
238  }
239
240  // assignment case is superseded by dr2180
241}
242
243namespace dr1672 { // dr1672: 7
244  struct Empty {};
245  struct A : Empty {};
246  struct B { Empty e; };
247  struct C : A { B b; int n; };
248  struct D : A { int n; B b; };
249
250  static_assert(!__is_standard_layout(C), "");
251  static_assert(__is_standard_layout(D), "");
252
253  struct E { B b; int n; };
254  struct F { int n; B b; };
255  union G { B b; int n; };
256  union H { int n; B b; };
257
258  struct X {};
259  template<typename T> struct Y : X, A { T t; };
260
261  static_assert(!__is_standard_layout(Y<E>), "");
262  static_assert(__is_standard_layout(Y<F>), "");
263  static_assert(!__is_standard_layout(Y<G>), "");
264  static_assert(!__is_standard_layout(Y<H>), "");
265  static_assert(!__is_standard_layout(Y<X>), "");
266}
267
268namespace dr1687 { // dr1687: 7
269  template<typename T> struct To {
270    operator T(); // expected-note 2{{first operand was implicitly converted to type 'int *'}}
271    // expected-note@-1 {{second operand was implicitly converted to type 'double'}}
272#if __cplusplus > 201703L
273    // expected-note@-3 2{{operand was implicitly converted to type 'dr1687::E}}
274#endif
275  };
276
277  int *a = To<int*>() + 100.0; // expected-error {{invalid operands to binary expression ('To<int *>' and 'double')}}
278  int *b = To<int*>() + To<double>(); // expected-error {{invalid operands to binary expression ('To<int *>' and 'To<double>')}}
279
280#if __cplusplus > 201703L
281  enum E1 {};
282  enum E2 {};
283  auto c = To<E1>() <=> To<E2>(); // expected-error {{invalid operands to binary expression ('To<dr1687::E1>' and 'To<dr1687::E2>')}}
284#endif
285}
286
287namespace dr1696 { // dr1696: 7
288  namespace std_examples {
289#if __cplusplus >= 201402L
290    extern struct A a;
291    struct A {
292      const A &x = { A{a, a} };
293      const A &y = { A{} }; // expected-error {{default member initializer for 'y' needed within definition of enclosing class 'A' outside of member functions}} expected-note {{here}}
294    };
295    A a{a, a};
296#endif
297  }
298
299  struct A { A(); ~A(); };
300#if __cplusplus >= 201103L
301  struct B {
302    A &&a; // expected-note {{declared here}}
303    B() : a{} {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
304  } b;
305#endif
306
307  struct C {
308    C();
309    const A &a; // expected-note {{declared here}}
310  };
311  C::C() : a(A()) {} // expected-error {{reference member 'a' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}}
312
313#if __cplusplus >= 201103L
314  // This is OK in C++14 onwards, per DR1815, though we don't support that yet:
315  //   D1 d1 = {};
316  // is equivalent to
317  //   D1 d1 = {A()};
318  // ... which lifetime-extends the A temporary.
319  struct D1 {
320#if __cplusplus < 201402L
321    // expected-error@-2 {{binds to a temporary}}
322#endif
323    const A &a = A(); // expected-note {{default member init}}
324  };
325  D1 d1 = {};
326#if __cplusplus < 201402L
327    // expected-note@-2 {{first required here}}
328#else
329    // expected-warning-re@-4 {{sorry, lifetime extension {{.*}} not supported}}
330#endif
331
332  struct D2 {
333    const A &a = A(); // expected-note {{default member init}}
334    D2() {} // expected-error {{binds to a temporary}}
335  };
336
337  struct D3 { // expected-error {{binds to a temporary}}
338    const A &a = A(); // expected-note {{default member init}}
339  };
340  D3 d3; // expected-note {{first required here}}
341
342  struct haslist1 {
343    std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
344    haslist1(int i) : il{i, 2, 3} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
345  };
346
347  struct haslist2 {
348    std::initializer_list<int> il; // expected-note {{'std::initializer_list' member}}
349    haslist2();
350  };
351  haslist2::haslist2() : il{1, 2} {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
352
353  struct haslist3 {
354    std::initializer_list<int> il = {1, 2, 3};
355  };
356
357  struct haslist4 { // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
358    std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
359  };
360  haslist4 hl4; // expected-note {{in implicit default constructor}}
361
362  struct haslist5 {
363    std::initializer_list<int> il = {1, 2, 3}; // expected-note {{default member initializer}}
364    haslist5() {} // expected-error {{backing array for 'std::initializer_list' member 'il' is a temporary object}}
365  };
366#endif
367}
368