Clang Project

clang_source_code/test/SemaCXX/new-delete-cxx0x.cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 -triple=i686-pc-linux-gnu -pedantic
2
3void ugly_news(int *ip) {
4  (void)new int[-1]; // expected-error {{array size is negative}}
5  (void)new int[2000000000]; // expected-error {{array is too large}}
6}
7
8void pr22845a() {
9  constexpr int i = -1;
10  int *p = new int[i]; // expected-error {{array size is negative}}
11}
12
13void pr22845b() {
14  constexpr int i = 1;
15  int *p = new int[i]{1, 2}; // expected-error {{excess elements in array initializer}}
16}
17
18struct S {
19  S(int);
20  S();
21  ~S();
22};
23
24struct T { // expected-note 1+{{not viable}}
25  T(int); // expected-note 1+{{not viable}}
26};
27
28void fn(int n) {
29  (void) new int[2] {1, 2};
30  (void) new S[2] {1, 2};
31  (void) new S[3] {1, 2};
32  (void) new S[n] {};
33  // C++11 [expr.new]p19:
34  //   If the new-expression creates an object or an array of objects of class
35  //   type, access and ambiguity control are done for the allocation function,
36  //   the deallocation function (12.5), and the constructor (12.1).
37  //
38  // Note that this happens even if the array bound is constant and the
39  // initializer initializes every array element.
40  //
41  // It's not clear that this is the intended interpretation, however -- we
42  // obviously don't want to check for a default constructor for 'new S(0)'.
43  // Instead, we only check for a default constructor in the case of an array
44  // new with a non-constant bound or insufficient initializers.
45  (void) new T[2] {1, 2}; // ok
46  (void) new T[3] {1, 2}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of array element 2}}
47  (void) new T[n] {1, 2}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of trailing array elements in runtime-sized array new}}
48  (void) new T[n] {}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of trailing array elements in runtime-sized array new}}
49}
50
51struct U {
52  T t; // expected-note 3{{in implicit initialization of field 't'}}
53  S s;
54};
55void g(int n) {
56  // Aggregate initialization, brace-elision, and array new combine to create
57  // this monstrosity.
58  (void) new U[2] {1, 2}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of array element 1}}
59  (void) new U[2] {1, 2, 3}; // ok
60  (void) new U[2] {1, 2, 3, 4}; // ok
61  (void) new U[2] {1, 2, 3, 4, 5}; // expected-error {{excess elements in array initializer}}
62
63  (void) new U[n] {1, 2}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of trailing array elements}}
64  (void) new U[n] {1, 2, 3}; // expected-error {{no matching constructor}} expected-note {{in implicit initialization of trailing array elements}}
65}
66