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