1 // { dg-options "-fno-lifetime-dse -O0" }
2 // { dg-do run { target c++20 } }
3 // { dg-xfail-run-if "AIX operator new" { powerpc-ibm-aix* } }
4 // { dg-require-effective-target hosted }
5 // { dg-add-options no_pch }
7 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
9 // This file is part of the GNU ISO C++ Library. This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
24 // C++20 20.11.1.5 unique_ptr creation [unique.ptr.create]
28 #ifndef __cpp_lib_smart_ptr_for_overwrite
29 # error "Feature-test macro for make_unique_for_overwrite missing in <memory>"
30 #elif __cpp_lib_smart_ptr_for_overwrite < 202002L
31 # error "Feature-test macro for make_unique_for_overwrite has wrong value in <memory>"
36 #include <testsuite_hooks.h>
38 void* operator new(std::size_t n
)
40 void* p
= std::malloc(n
);
41 std::memset(p
, 0xaa, n
);
45 void operator delete(void* p
) noexcept
{ std::free(p
); }
46 void operator delete(void* p
, std::size_t) noexcept
{ std::free(p
); }
51 std::unique_ptr
<int> a
= std::make_unique_for_overwrite
<int>();
52 VERIFY( a
!= nullptr );
53 unsigned char buf
[sizeof(int)];
54 std::memcpy(buf
, a
.get(), sizeof(buf
));
55 for (unsigned char c
: buf
)
62 std::unique_ptr
<int[]> a
= std::make_unique_for_overwrite
<int[]>(3);
63 VERIFY( a
!= nullptr );
64 unsigned char buf
[3 * sizeof(int)];
65 std::memcpy(buf
, a
.get(), sizeof(buf
));
66 for (unsigned char c
: buf
)
73 // Type with non-trivial initialization should still be default-initialized.
79 std::unique_ptr
<NonTriv
> a
= std::make_unique_for_overwrite
<NonTriv
>();
80 VERIFY( a
->init
== 0xbb );
81 std::unique_ptr
<NonTriv
[]> b
= std::make_unique_for_overwrite
<NonTriv
[]>(2);
82 VERIFY( b
[1].init
== 0xbb );