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