| 1 |
// PR c++/115645 |
| 2 |
// { dg-do compile { target c++20 } } |
| 3 |
|
| 4 |
using size_t = decltype(sizeof(0)); |
| 5 |
|
| 6 |
void* operator new(size_t, void* p) { return p; } |
| 7 |
void* operator new[](size_t, void* p) { return p; } |
| 8 |
|
| 9 |
#define VERIFY(C) if (!(C)) throw |
| 10 |
|
| 11 |
namespace std { |
| 12 |
template<typename T> |
| 13 |
constexpr T* construct_at(T* p) |
| 14 |
{ |
| 15 |
if constexpr (__is_array(T)) |
| 16 |
return ::new((void*)p) T[1](); |
| 17 |
else |
| 18 |
return ::new((void*)p) T(); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
constexpr void |
| 23 |
test_array() |
| 24 |
{ |
| 25 |
int arr[1] { 99 }; |
| 26 |
std::construct_at(&arr); |
| 27 |
VERIFY( arr[0] == 0 ); |
| 28 |
|
| 29 |
union U { |
| 30 |
long long x = -1; |
| 31 |
int arr[4]; |
| 32 |
} u; |
| 33 |
|
| 34 |
auto p = std::construct_at(&u.arr); |
| 35 |
VERIFY( (*p)[0] == 0 ); |
| 36 |
} |
| 37 |
|
| 38 |
static_assert( [] { test_array(); return true; }() ); |