constexpr bool alloc_from_user_code() {
void *p = NEW(sizeof(int)); DELETE(p);
return true;
}
static_assert(alloc_from_user_code());
namespace std {
using size_t = decltype(sizeof(0));
template<typename T> struct allocator {
constexpr T *allocate(size_t N) {
return (T*)NEW(sizeof(T) * N); }
constexpr void deallocate(void *p) {
DELETE(p); }
};
}
constexpr bool alloc_via_std_allocator() {
std::allocator<int> alloc;
int *p = alloc.allocate(1);
alloc.deallocate(p);
return true;
}
static_assert(alloc_via_std_allocator());
template<> struct std::allocator<void()> {
constexpr void *allocate() { return NEW(8); } };
constexpr void *fn = std::allocator<void()>().allocate();
struct Incomplete;
template<> struct std::allocator<Incomplete> {
constexpr void *allocate() { return NEW(8); } };
constexpr void *incomplete = std::allocator<Incomplete>().allocate();
struct WrongSize { char x[5]; };
static_assert(sizeof(WrongSize) == 5);
template<> struct std::allocator<WrongSize> {
constexpr void *allocate() { return NEW(7); } };
constexpr void *wrong_size = std::allocator<WrongSize>().allocate();
constexpr bool mismatched(int alloc_kind, int dealloc_kind) {
int *p;
switch (alloc_kind) {
case 0:
p = new int; break;
case 1:
p = new int[1]; break;
case 2:
p = std::allocator<int>().allocate(1);
break;
}
switch (dealloc_kind) {
case 0:
delete p; break;
case 1:
delete[] p; break;
case 2:
std::allocator<int>().deallocate(p); break;
}
return true;
}
static_assert(mismatched(0, 2)); static_assert(mismatched(1, 2)); static_assert(mismatched(2, 0)); static_assert(mismatched(2, 1)); static_assert(mismatched(2, 2));
constexpr int *escape = std::allocator<int>().allocate(3); constexpr int leak = (std::allocator<int>().allocate(3), 0); constexpr int no_lifetime_start = (*std::allocator<int>().allocate(1) = 1); constexpr int no_deallocate_nullptr = (std::allocator<int>().deallocate(nullptr), 1); constexpr int no_deallocate_nonalloc = (std::allocator<int>().deallocate((int*)&no_deallocate_nonalloc), 1);
void *operator new(std::size_t, void *p) { return p; }
constexpr bool no_placement_new_in_user_code() { int a;
new (&a) int(42); return a == 42;
}
namespace std {
constexpr bool placement_new_in_stdlib() {
int a;
new (&a) int(42);
return a == 42;
}
}
static_assert(std::placement_new_in_stdlib());
namespace std {
template<typename T, typename ...Args>
constexpr void construct_at(void *p, Args &&...args) {
new (p) T((Args&&)args...); }
}
constexpr bool call_std_construct_at() {
int *p = std::allocator<int>().allocate(3);
std::construct_at<int>(p, 1);
std::construct_at<int>(p + 1, 2);
std::construct_at<int>(p + 2, 3);
bool good = p[0] + p[1] + p[2] == 6;
std::allocator<int>().deallocate(p);
return good;
}
static_assert(call_std_construct_at());
constexpr bool bad_construct_at_type() {
int a;
std::construct_at<float>(&a, 1.0f); return true;
}
static_assert(bad_construct_at_type());
constexpr bool bad_construct_at_subobject() {
struct X { int a, b; };
union A {
int a;
X x;
};
A a = {1};
std::construct_at<int>(&a.x.a, 1); return true;
}
static_assert(bad_construct_at_subobject());
constexpr bool change_union_member() {
union U {
int a;
int b;
};
U u = {.a = 1};
std::construct_at<int>(&u.b, 2);
return u.b == 2;
}
static_assert(change_union_member());
int external;
static_assert((std::construct_at<int>(&external, 1), true));
constexpr int &&temporary = 0; static_assert((std::construct_at<int>(&temporary, 1), true));
constexpr bool construct_after_lifetime() {
int *p = new int;
delete p;
std::construct_at<int>(p); return true;
}
static_assert(construct_after_lifetime());
constexpr bool construct_after_lifetime_2() {
struct A { struct B {} b; };
A a;
a.~A();
std::construct_at<A::B>(&a.b); return true;
}
static_assert(construct_after_lifetime_2());
namespace PR48606 {
struct A { mutable int n = 0; };
constexpr bool f() {
A a;
A *p = &a;
p->~A();
std::construct_at<A>(p);
return true;
}
static_assert(f());
constexpr bool g() {
A *p = new A;
p->~A();
std::construct_at<A>(p);
delete p;
return true;
}
static_assert(g());
constexpr bool h() {
std::allocator<A> alloc;
A *p = alloc.allocate(1);
std::construct_at<A>(p);
p->~A();
std::construct_at<A>(p);
p->~A();
alloc.deallocate(p);
return true;
}
static_assert(h());
}