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