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 //=============================================================================
14 // TESTING std::unique_ptr::unique_ptr()
15 //
16 // Concerns:
17 //   1 The default constructor works for any default constructible deleter types.
18 //   2 The stored type 'T' is allowed to be incomplete.
19 //
20 // Plan
21 //  1 Default construct unique_ptr's with various deleter types (C-1)
22 //  2 Default construct a unique_ptr with an incomplete element_type and
23 //    various deleter types (C-1,2)
24 
25 #include <memory>
26 #include <cassert>
27 #include "test_macros.h"
28 
29 #include "test_macros.h"
30 #include "deleter_types.h"
31 #include "unique_ptr_test_helper.h"
32 
33 #if defined(_LIBCPP_VERSION) && TEST_STD_VER >= 11
34 _LIBCPP_SAFE_STATIC std::unique_ptr<int> global_static_unique_ptr_single;
35 _LIBCPP_SAFE_STATIC std::unique_ptr<int[]> global_static_unique_ptr_runtime;
36 #endif
37 
38 #if TEST_STD_VER >= 11
39 struct NonDefaultDeleter {
40   NonDefaultDeleter() = delete;
41   void operator()(void*) const {}
42 };
43 #endif
44 
45 template <class ElemType>
46 void test_sfinae() {
47 #if TEST_STD_VER >= 11
48   { // the constructor does not participate in overload resolution when
49     // the deleter is a pointer type
50     using U = std::unique_ptr<ElemType, void (*)(void*)>;
51     static_assert(!std::is_default_constructible<U>::value, "");
52   }
53   { // the constructor does not participate in overload resolution when
54     // the deleter is not default constructible
55     using Del = CDeleter<ElemType>;
56     using U1 = std::unique_ptr<ElemType, NonDefaultDeleter>;
57     using U2 = std::unique_ptr<ElemType, Del&>;
58     using U3 = std::unique_ptr<ElemType, Del const&>;
59     static_assert(!std::is_default_constructible<U1>::value, "");
60     static_assert(!std::is_default_constructible<U2>::value, "");
61     static_assert(!std::is_default_constructible<U3>::value, "");
62   }
63 #endif
64 }
65 
66 template <class ElemType>
67 void test_basic() {
68 #if TEST_STD_VER >= 11
69   {
70     using U1 = std::unique_ptr<ElemType>;
71     using U2 = std::unique_ptr<ElemType, Deleter<ElemType> >;
72     static_assert(std::is_nothrow_default_constructible<U1>::value, "");
73     static_assert(std::is_nothrow_default_constructible<U2>::value, "");
74   }
75 #endif
76   {
77     std::unique_ptr<ElemType> p;
78     assert(p.get() == 0);
79   }
80   {
81     std::unique_ptr<ElemType, NCDeleter<ElemType> > p;
82     assert(p.get() == 0);
83     assert(p.get_deleter().state() == 0);
84     p.get_deleter().set_state(5);
85     assert(p.get_deleter().state() == 5);
86   }
87 }
88 
89 DEFINE_AND_RUN_IS_INCOMPLETE_TEST({
90   doIncompleteTypeTest(0);
91   doIncompleteTypeTest<IncompleteType, Deleter<IncompleteType> >(0);
92 } {
93   doIncompleteTypeTest<IncompleteType[]>(0);
94   doIncompleteTypeTest<IncompleteType[], Deleter<IncompleteType[]> >(0);
95 })
96 
97 int main(int, char**) {
98   {
99     test_sfinae<int>();
100     test_basic<int>();
101   }
102   {
103     test_sfinae<int[]>();
104     test_basic<int[]>();
105   }
106 
107   return 0;
108 }
109