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 // <memory>
10
11 // unique_ptr
12
13 // unique_ptr(nullptr_t);
14
15 #include <memory>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "unique_ptr_test_helper.h"
20
21
22 #if TEST_STD_VER >= 11
23 TEST_CONSTINIT std::unique_ptr<int> global_static_unique_ptr_single(nullptr);
24 TEST_CONSTINIT std::unique_ptr<int[]> global_static_unique_ptr_runtime(nullptr);
25
26 struct NonDefaultDeleter {
27 NonDefaultDeleter() = delete;
operator ()NonDefaultDeleter28 void operator()(void*) const {}
29 };
30 #endif
31
32 template <class VT>
test_basic()33 void test_basic() {
34 #if TEST_STD_VER >= 11
35 {
36 using U1 = std::unique_ptr<VT>;
37 using U2 = std::unique_ptr<VT, Deleter<VT> >;
38 static_assert(std::is_nothrow_constructible<U1, decltype(nullptr)>::value,
39 "");
40 static_assert(std::is_nothrow_constructible<U2, decltype(nullptr)>::value,
41 "");
42 }
43 #endif
44 {
45 std::unique_ptr<VT> p(nullptr);
46 assert(p.get() == 0);
47 }
48 {
49 std::unique_ptr<VT, NCDeleter<VT> > p(nullptr);
50 assert(p.get() == 0);
51 assert(p.get_deleter().state() == 0);
52 }
53 {
54 std::unique_ptr<VT, DefaultCtorDeleter<VT> > p(nullptr);
55 assert(p.get() == 0);
56 assert(p.get_deleter().state() == 0);
57 }
58 }
59
60 template <class VT>
test_sfinae()61 void test_sfinae() {
62 #if TEST_STD_VER >= 11
63 { // the constructor does not participate in overload resolution when
64 // the deleter is a pointer type
65 using U = std::unique_ptr<VT, void (*)(void*)>;
66 static_assert(!std::is_constructible<U, decltype(nullptr)>::value, "");
67 }
68 { // the constructor does not participate in overload resolution when
69 // the deleter is not default constructible
70 using Del = CDeleter<VT>;
71 using U1 = std::unique_ptr<VT, NonDefaultDeleter>;
72 using U2 = std::unique_ptr<VT, Del&>;
73 using U3 = std::unique_ptr<VT, Del const&>;
74 static_assert(!std::is_constructible<U1, decltype(nullptr)>::value, "");
75 static_assert(!std::is_constructible<U2, decltype(nullptr)>::value, "");
76 static_assert(!std::is_constructible<U3, decltype(nullptr)>::value, "");
77 }
78 #endif
79 }
80
81 DEFINE_AND_RUN_IS_INCOMPLETE_TEST({
82 { doIncompleteTypeTest(0, nullptr); }
83 checkNumIncompleteTypeAlive(0);
84 {
85 doIncompleteTypeTest<IncompleteType, NCDeleter<IncompleteType> >(0,
86 nullptr);
87 }
88 checkNumIncompleteTypeAlive(0);
89 { doIncompleteTypeTest<IncompleteType[]>(0, nullptr); }
90 checkNumIncompleteTypeAlive(0);
91 {
92 doIncompleteTypeTest<IncompleteType[], NCDeleter<IncompleteType[]> >(
93 0, nullptr);
94 }
95 checkNumIncompleteTypeAlive(0);
96 })
97
main(int,char **)98 int main(int, char**) {
99 {
100 test_basic<int>();
101 test_sfinae<int>();
102 }
103 {
104 test_basic<int[]>();
105 test_sfinae<int[]>();
106 }
107
108 return 0;
109 }
110