| 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 } |
| 6 | |
| 7 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 8 | // |
| 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) |
| 13 | // any later version. |
| 14 | |
| 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. |
| 19 | |
| 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/>. |
| 23 | |
| 24 | // C++20 20.11.1.5 unique_ptr creation [unique.ptr.create] |
| 25 | |
| 26 | #include <memory> |
| 27 | |
| 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>" |
| 32 | #endif |
| 33 | |
| 34 | #include <cstdlib> |
| 35 | #include <cstring> |
| 36 | #include <testsuite_hooks.h> |
| 37 | |
| 38 | void* operator new(std::size_t n) |
| 39 | { |
| 40 | void* p = std::malloc(n); |
| 41 | std::memset(p, 0xaa, n); |
| 42 | return p; |
| 43 | } |
| 44 | |
| 45 | void operator delete(void* p) noexcept { std::free(p); } |
| 46 | void operator delete(void* p, std::size_t) noexcept { std::free(p); } |
| 47 | |
| 48 | void |
| 49 | test01() |
| 50 | { |
| 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) |
| 56 | VERIFY( c == 0xaa ); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | test02() |
| 61 | { |
| 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) |
| 67 | VERIFY( c == 0xaa ); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | test03() |
| 72 | { |
| 73 | // Type with non-trivial initialization should still be default-initialized. |
| 74 | struct NonTriv |
| 75 | { |
| 76 | int init = 0xbb; |
| 77 | int uninit; |
| 78 | }; |
| 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 ); |
| 83 | } |
| 84 | |
| 85 | int |
| 86 | main() |
| 87 | { |
| 88 | test01(); |
| 89 | test02(); |
| 90 | test03(); |
| 91 | } |