1 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s -DNEW=__builtin_operator_new -DDELETE=__builtin_operator_delete
2 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=operator new" "-DDELETE=operator delete"
3 // RUN: %clang_cc1 -std=c++2a -verify=expected,cxx20 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
4 // RUN: %clang_cc1 -std=c++2c -verify=expected,cxx26 %s "-DNEW=::operator new" "-DDELETE=::operator delete"
6 constexpr bool alloc_from_user_code() {
7 void *p
= NEW(sizeof(int)); // expected-note {{cannot allocate untyped memory in a constant expression; use 'std::allocator<T>::allocate'}}
11 static_assert(alloc_from_user_code()); // expected-error {{constant expression}} expected-note {{in call}}
14 using size_t = decltype(sizeof(0));
15 // FIXME: It would be preferable to point these notes at the location of the call to allocator<...>::[de]allocate instead
16 template<typename T
> struct allocator
{
17 constexpr T
*allocate(size_t N
) {
18 return (T
*)NEW(sizeof(T
) * N
); // expected-note 3{{heap allocation}} expected-note {{not deallocated}}
20 constexpr void deallocate(void *p
) {
21 DELETE(p
); // #dealloc expected-note 2{{'std::allocator<...>::deallocate' used to delete pointer to object allocated with 'new'}}
26 constexpr bool alloc_via_std_allocator() {
27 std::allocator
<int> alloc
;
28 int *p
= alloc
.allocate(1);
32 static_assert(alloc_via_std_allocator());
34 template<> struct std::allocator
<void()> {
35 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of function type 'void ()'}}
37 constexpr void *fn
= std::allocator
<void()>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
40 template<> struct std::allocator
<Incomplete
> {
41 constexpr void *allocate() { return NEW(8); } // expected-note {{cannot allocate memory of incomplete type 'Incomplete'}}
43 constexpr void *incomplete
= std::allocator
<Incomplete
>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
45 struct WrongSize
{ char x
[5]; };
46 static_assert(sizeof(WrongSize
) == 5);
47 template<> struct std::allocator
<WrongSize
> {
48 constexpr void *allocate() { return NEW(7); } // expected-note {{allocated size 7 is not a multiple of size 5 of element type 'WrongSize'}}
50 constexpr void *wrong_size
= std::allocator
<WrongSize
>().allocate(); // expected-error {{constant expression}} expected-note {{in call}}
52 constexpr bool mismatched(int alloc_kind
, int dealloc_kind
) {
56 p
= new int; // expected-note {{heap allocation}}
59 p
= new int[1]; // expected-note {{heap allocation}}
62 p
= std::allocator
<int>().allocate(1);
65 switch (dealloc_kind
) {
67 delete p
; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
70 delete[] p
; // expected-note {{'delete' used to delete pointer to object allocated with 'std::allocator<...>::allocate'}}
73 std::allocator
<int>().deallocate(p
); // expected-note 2{{in call}}
78 static_assert(mismatched(0, 2)); // expected-error {{constant expression}} expected-note {{in call}}
79 static_assert(mismatched(1, 2)); // expected-error {{constant expression}} expected-note {{in call}}
80 static_assert(mismatched(2, 0)); // expected-error {{constant expression}} expected-note {{in call}}
81 static_assert(mismatched(2, 1)); // expected-error {{constant expression}} expected-note {{in call}}
82 static_assert(mismatched(2, 2));
84 constexpr int *escape
= std::allocator
<int>().allocate(3); // expected-error {{constant expression}} expected-note {{pointer to subobject of heap-allocated}}
85 constexpr int leak
= (std::allocator
<int>().allocate(3), 0); // expected-error {{constant expression}}
86 constexpr int no_lifetime_start
= (*std::allocator
<int>().allocate(1) = 1); // expected-error {{constant expression}} expected-note {{assignment to object outside its lifetime}}
87 constexpr int no_deallocate_nullptr
= (std::allocator
<int>().deallocate(nullptr), 1); // expected-error {{constant expression}} expected-note {{in call}}
88 // expected-note@#dealloc {{'std::allocator<...>::deallocate' used to delete a null pointer}}
89 constexpr int no_deallocate_nonalloc
= (std::allocator
<int>().deallocate((int*)&no_deallocate_nonalloc
), 1); // expected-error {{constant expression}} expected-note {{in call}}
90 // expected-note@#dealloc {{delete of pointer '&no_deallocate_nonalloc' that does not point to a heap-allocated object}}
91 // expected-note@-2 {{declared here}}
93 void *operator new(std::size_t, void *p
) { return p
; }
94 void* operator new[] (std::size_t, void* p
) {return p
;}
95 constexpr bool no_placement_new_in_user_code() { // cxx20-error {{constexpr function never produces a constant expression}}
97 new (&a
) int(42); // cxx20-note {{this placement new expression is not supported in constant expressions before C++2c}}
102 constexpr bool placement_new_in_stdlib() {
108 static_assert(std::placement_new_in_stdlib());
111 template<typename T
, typename
...Args
>
112 constexpr void construct_at(void *p
, Args
&&...args
) {
113 new (p
) T((Args
&&)args
...); // #new
117 constexpr bool call_std_construct_at() {
118 int *p
= std::allocator
<int>().allocate(3);
119 std::construct_at
<int>(p
, 1);
120 std::construct_at
<int>(p
+ 1, 2);
121 std::construct_at
<int>(p
+ 2, 3);
122 bool good
= p
[0] + p
[1] + p
[2] == 6;
123 std::allocator
<int>().deallocate(p
);
126 static_assert(call_std_construct_at());
128 constexpr bool bad_construct_at_type() {
130 // expected-note@#new {{placement new would change type of storage from 'int' to 'float'}}
131 std::construct_at
<float>(&a
, 1.0f
); // expected-note {{in call}}
134 static_assert(bad_construct_at_type()); // expected-error{{}} expected-note {{in call}}
136 constexpr bool bad_construct_at_subobject() {
137 struct X
{ int a
, b
; };
143 // expected-note@#new {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
144 std::construct_at
<int>(&a
.x
.a
, 1); // expected-note {{in call}}
147 static_assert(bad_construct_at_subobject()); // expected-error{{}} expected-note {{in call}}
149 constexpr bool change_union_member() {
155 std::construct_at
<int>(&u
.b
, 2);
158 static_assert(change_union_member());
161 // expected-note@#new {{visible outside}}
162 static_assert((std::construct_at
<int>(&external
, 1), true)); // expected-error{{}} expected-note {{in call}}
164 constexpr int &&temporary
= 0; // expected-note {{created here}}
165 // expected-note@#new {{construction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
166 static_assert((std::construct_at
<int>(&temporary
, 1), true)); // expected-error{{}} expected-note {{in call}}
168 constexpr bool construct_after_lifetime() {
171 // expected-note@#new {{construction of heap allocated object that has been deleted}}
172 std::construct_at
<int>(p
); // expected-note {{in call}}
175 static_assert(construct_after_lifetime()); // expected-error {{}} expected-note {{in call}}
177 constexpr bool construct_after_lifetime_2() {
178 struct A
{ struct B
{} b
; };
181 std::construct_at
<A::B
>(&a
.b
); // expected-note {{in call}}
182 // expected-note@#new {{construction of subobject of object outside its lifetime is not allowed in a constant expression}}
185 static_assert(construct_after_lifetime_2()); // expected-error {{}} expected-note {{in call}}
188 struct A
{ mutable int n
= 0; };
194 std::construct_at
<A
>(p
);
202 std::construct_at
<A
>(p
);
209 std::allocator
<A
> alloc
;
210 A
*p
= alloc
.allocate(1);
211 std::construct_at
<A
>(p
);
213 std::construct_at
<A
>(p
);
227 this->mem
= new char(1);
229 constexpr ~string() {
232 constexpr unsigned size() const { return 4; }
236 template <unsigned N
>
240 test
<string().size()>();