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