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 std::unique_ptr<VT, DefaultCtorDeleter<VT> > p(nullptr); 58 assert(p.get() == 0); 59 assert(p.get_deleter().state() == 0); 60 } 61 } 62 63 template <class VT> 64 void test_sfinae() { 65 #if TEST_STD_VER >= 11 66 { // the constructor does not participate in overload resolution when 67 // the deleter is a pointer type 68 using U = std::unique_ptr<VT, void (*)(void*)>; 69 static_assert(!std::is_constructible<U, decltype(nullptr)>::value, ""); 70 } 71 { // the constructor does not participate in overload resolution when 72 // the deleter is not default constructible 73 using Del = CDeleter<VT>; 74 using U1 = std::unique_ptr<VT, NonDefaultDeleter>; 75 using U2 = std::unique_ptr<VT, Del&>; 76 using U3 = std::unique_ptr<VT, Del const&>; 77 static_assert(!std::is_constructible<U1, decltype(nullptr)>::value, ""); 78 static_assert(!std::is_constructible<U2, decltype(nullptr)>::value, ""); 79 static_assert(!std::is_constructible<U3, decltype(nullptr)>::value, ""); 80 } 81 #endif 82 } 83 84 DEFINE_AND_RUN_IS_INCOMPLETE_TEST({ 85 { doIncompleteTypeTest(0, nullptr); } 86 checkNumIncompleteTypeAlive(0); 87 { 88 doIncompleteTypeTest<IncompleteType, NCDeleter<IncompleteType> >(0, 89 nullptr); 90 } 91 checkNumIncompleteTypeAlive(0); 92 { doIncompleteTypeTest<IncompleteType[]>(0, nullptr); } 93 checkNumIncompleteTypeAlive(0); 94 { 95 doIncompleteTypeTest<IncompleteType[], NCDeleter<IncompleteType[]> >( 96 0, nullptr); 97 } 98 checkNumIncompleteTypeAlive(0); 99 }) 100 101 int main(int, char**) { 102 { 103 test_basic<int>(); 104 test_sfinae<int>(); 105 } 106 { 107 test_basic<int[]>(); 108 test_sfinae<int[]>(); 109 } 110 111 return 0; 112 } 113