114 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// <memory>10 11// unique_ptr12 13// constexpr unique_ptr(nullptr_t); // constexpr since C++2314 15#include <memory>16#include <cassert>17 18#include "test_macros.h"19#include "unique_ptr_test_helper.h"20 21#if TEST_STD_VER >= 1122TEST_CONSTINIT std::unique_ptr<int> global_static_unique_ptr_single(nullptr);23TEST_CONSTINIT std::unique_ptr<int[]> global_static_unique_ptr_runtime(nullptr);24 25struct NonDefaultDeleter {26 NonDefaultDeleter() = delete;27 void operator()(void*) const {}28};29#endif30 31template <class VT>32TEST_CONSTEXPR_CXX23 void test_basic() {33#if TEST_STD_VER >= 1134 {35 using U1 = std::unique_ptr<VT>;36 using U2 = std::unique_ptr<VT, Deleter<VT> >;37 static_assert(std::is_nothrow_constructible<U1, decltype(nullptr)>::value, "");38 static_assert(std::is_nothrow_constructible<U2, decltype(nullptr)>::value, "");39 }40#endif41 {42 std::unique_ptr<VT> p(nullptr);43 assert(p.get() == 0);44 }45 {46 std::unique_ptr<VT, NCDeleter<VT> > p(nullptr);47 assert(p.get() == 0);48 assert(p.get_deleter().state() == 0);49 }50// TODO: Remove this check once https://llvm.org/PR154567 is fixed51#if TEST_STD_VER >= 23 && defined(TEST_COMPILER_CLANG)52 if (!TEST_IS_CONSTANT_EVALUATED)53#endif54 {55 std::unique_ptr<VT, DefaultCtorDeleter<VT> > p(nullptr);56 assert(p.get() == 0);57 assert(p.get_deleter().state() == 0);58 }59}60 61template <class VT>62TEST_CONSTEXPR_CXX23 void test_sfinae() {63#if TEST_STD_VER >= 1164 { // the constructor does not participate in overload resolution when65 // the deleter is a pointer type66 using U = std::unique_ptr<VT, void (*)(void*)>;67 static_assert(!std::is_constructible<U, decltype(nullptr)>::value, "");68 }69 { // the constructor does not participate in overload resolution when70 // the deleter is not default constructible71 using Del = CDeleter<VT>;72 using U1 = std::unique_ptr<VT, NonDefaultDeleter>;73 using U2 = std::unique_ptr<VT, Del&>;74 using U3 = std::unique_ptr<VT, Del const&>;75 static_assert(!std::is_constructible<U1, decltype(nullptr)>::value, "");76 static_assert(!std::is_constructible<U2, decltype(nullptr)>::value, "");77 static_assert(!std::is_constructible<U3, decltype(nullptr)>::value, "");78 }79#endif80}81 82DEFINE_AND_RUN_IS_INCOMPLETE_TEST({83 { doIncompleteTypeTest(0, nullptr); }84 checkNumIncompleteTypeAlive(0);85 { doIncompleteTypeTest<IncompleteType, NCDeleter<IncompleteType> >(0, nullptr); }86 checkNumIncompleteTypeAlive(0);87 { doIncompleteTypeTest<IncompleteType[]>(0, nullptr); }88 checkNumIncompleteTypeAlive(0);89 { doIncompleteTypeTest<IncompleteType[], NCDeleter<IncompleteType[]> >(0, nullptr); }90 checkNumIncompleteTypeAlive(0);91})92 93TEST_CONSTEXPR_CXX23 bool test() {94 {95 test_basic<int>();96 test_sfinae<int>();97 }98 {99 test_basic<int[]>();100 test_sfinae<int[]>();101 }102 103 return true;104}105 106int main(int, char**) {107 test();108#if TEST_STD_VER >= 23109 static_assert(test());110#endif111 112 return 0;113}114