1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14, c++17
10// UNSUPPORTED: sanitizer-new-delete
11
12// template<class T>
13// constexpr unique_ptr<T> make_unique_for_overwrite(); // T is not array
14//
15// template<class T>
16// constexpr unique_ptr<T> make_unique_for_overwrite(size_t n); // T is U[]
17
18// Test the object is not value initialized
19
20#include <cassert>
21#include <concepts>
22#include <cstddef>
23#include <cstdlib>
24#include <memory>
25
26constexpr char pattern = static_cast<char>(0xDE);
27
28void* operator new(std::size_t count) {
29 void* ptr = std::malloc(size: count);
30 if (!ptr) {
31 std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
32 }
33 for (std::size_t i = 0; i < count; ++i) {
34 *(reinterpret_cast<char*>(ptr) + i) = pattern;
35 }
36 return ptr;
37}
38
39void* operator new[](std::size_t count) { return ::operator new(count); }
40
41void operator delete(void* ptr) noexcept { std::free(ptr: ptr); }
42
43void operator delete[](void* ptr) noexcept { ::operator delete(ptr); }
44
45void test() {
46 {
47 std::same_as<std::unique_ptr<int>> auto ptr = std::make_unique_for_overwrite<int>();
48 assert(*(reinterpret_cast<char*>(ptr.get())) == pattern);
49 }
50 {
51 std::same_as<std::unique_ptr<int[]>> auto ptr = std::make_unique_for_overwrite<int[]>(3);
52 assert(*(reinterpret_cast<char*>(&ptr[0])) == pattern);
53 assert(*(reinterpret_cast<char*>(&ptr[1])) == pattern);
54 assert(*(reinterpret_cast<char*>(&ptr[2])) == pattern);
55 }
56}
57
58int main(int, char**) {
59 test();
60
61 return 0;
62}
63

source code of libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.create/make_unique_for_overwrite.default_init.pass.cpp