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