1 // RUN: %clang_cc1 -std=c++2c -verify %s
5 using size_t = decltype(sizeof(0));
8 void *operator new(std::size_t, void *p
) { return p
; }
9 void* operator new[] (std::size_t, void* p
) {return p
;}
16 new (static_cast<void*>(&i
)) int(0);
20 consteval
int conversion() {
22 new (static_cast<void*>(&i
)) float(0);
23 // expected-note@-1 {{placement new would change type of storage from 'int' to 'float'}}
27 consteval
int indeterminate() {
29 new (indeterminate
) int(0);
30 // expected-note@-1 {{read of uninitialized object is not allowed in a constant expression}}
34 consteval
int array1() {
39 new (static_cast<void*>(&i
)) int[]{1,2};
40 new (static_cast<void*>(&i
)) int[]{1};
44 consteval
int array2() {
47 //expected-note@-1 {{placement new would change type of storage from 'int[1]' to 'int[2]'}}
53 constexpr S() : i(new int(42)) {} // #no-deallocation
54 constexpr ~S() {delete i
;}
57 consteval
void alloc() {
65 consteval
void alloc_err() {
74 int b
= conversion(); // expected-error {{call to consteval function 'conversion' is not a constant expression}} \
75 // expected-note {{in call to 'conversion()'}}
76 int c
= indeterminate(); // expected-error {{call to consteval function 'indeterminate' is not a constant expression}} \
77 // expected-note {{in call to 'indeterminate()'}}
79 int e
= array2(); // expected-error {{call to consteval function 'array2' is not a constant expression}} \
80 // expected-note {{in call to 'array2()'}}
81 int alloc1
= (alloc(), 0);
82 int alloc2
= (alloc_err(), 0); // expected-error {{call to consteval function 'alloc_err' is not a constant expression}}
83 // expected-note@#no-deallocation {{allocation performed here was not deallocated}}
85 constexpr int *intptr() {
89 constexpr bool yay() {
90 int *ptr
= new (intptr()) int(42);
91 bool ret
= *ptr
== 42;
97 constexpr bool blah() {
98 int *ptr
= new (intptr()) int[3]{ 1, 2, 3 }; // expected-note {{placement new would change type of storage from 'int' to 'int[3]'}}
99 bool ret
= ptr
[0] == 1 && ptr
[1] == 2 && ptr
[2] == 3;
103 static_assert(blah()); // expected-error {{not an integral constant expression}} \
104 // expected-note {{in call to 'blah()'}}
106 constexpr int *get_indeterminate() {
108 return evil
; // expected-note {{read of uninitialized object is not allowed in a constant expression}}
111 constexpr bool bleh() {
112 int *ptr
= new (get_indeterminate()) int; // expected-note {{in call to 'get_indeterminate()'}}
115 static_assert(bleh()); // expected-error {{not an integral constant expression}} \
116 // expected-note {{in call to 'bleh()'}}