1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// <memory>
// unique_ptr
//=============================================================================
// TESTING std::unique_ptr::unique_ptr(pointer)
//
// Concerns:
// 1 The pointer constructor works for any default constructible deleter types.
// 2 The pointer constructor accepts pointers to derived types.
// 2 The stored type 'T' is allowed to be incomplete.
//
// Plan
// 1 Construct unique_ptr<T, D>'s with a pointer to 'T' and various deleter
// types (C-1)
// 2 Construct unique_ptr<T, D>'s with a pointer to 'D' and various deleter
// types where 'D' is derived from 'T'. (C-1,2)
// 3 Construct a unique_ptr<T, D> with a pointer to 'T' and various deleter
// types where 'T' is an incomplete type (C-1,3)
// Test unique_ptr(pointer) ctor
#include <memory>
#include <cassert>
#include "test_macros.h"
#include "unique_ptr_test_helper.h"
// unique_ptr(pointer) ctor should only require default Deleter ctor
template <bool IsArray>
void test_pointer() {
typedef typename std::conditional<!IsArray, A, A[]>::type ValueT;
const int expect_alive = IsArray ? 5 : 1;
#if TEST_STD_VER >= 11
{
using U1 = std::unique_ptr<ValueT>;
using U2 = std::unique_ptr<ValueT, Deleter<ValueT> >;
// Test for noexcept
static_assert(std::is_nothrow_constructible<U1, A*>::value, "");
static_assert(std::is_nothrow_constructible<U2, A*>::value, "");
// Test for explicit
static_assert(!std::is_convertible<A*, U1>::value, "");
static_assert(!std::is_convertible<A*, U2>::value, "");
}
#endif
{
A* p = newValue<ValueT>(expect_alive);
assert(A::count == expect_alive);
std::unique_ptr<ValueT> s(p);
assert(s.get() == p);
}
assert(A::count == 0);
{
A* p = newValue<ValueT>(expect_alive);
assert(A::count == expect_alive);
std::unique_ptr<ValueT, NCDeleter<ValueT> > s(p);
assert(s.get() == p);
assert(s.get_deleter().state() == 0);
}
assert(A::count == 0);
}
void test_derived() {
{
B* p = new B;
assert(A::count == 1);
assert(B::count == 1);
std::unique_ptr<A> s(p);
assert(s.get() == p);
}
assert(A::count == 0);
assert(B::count == 0);
{
B* p = new B;
assert(A::count == 1);
assert(B::count == 1);
std::unique_ptr<A, NCDeleter<A> > s(p);
assert(s.get() == p);
assert(s.get_deleter().state() == 0);
}
assert(A::count == 0);
assert(B::count == 0);
}
#if TEST_STD_VER >= 11
struct NonDefaultDeleter {
NonDefaultDeleter() = delete;
void operator()(void*) const {}
};
struct GenericDeleter {
void operator()(void*) const;
};
#endif
template <class T>
void test_sfinae() {
#if TEST_STD_VER >= 11
{ // the constructor does not participate in overload resolution when
// the deleter is a pointer type
using U = std::unique_ptr<T, void (*)(void*)>;
static_assert(!std::is_constructible<U, T*>::value, "");
}
{ // the constructor does not participate in overload resolution when
// the deleter is not default constructible
using Del = CDeleter<T>;
using U1 = std::unique_ptr<T, NonDefaultDeleter>;
using U2 = std::unique_ptr<T, Del&>;
using U3 = std::unique_ptr<T, Del const&>;
static_assert(!std::is_constructible<U1, T*>::value, "");
static_assert(!std::is_constructible<U2, T*>::value, "");
static_assert(!std::is_constructible<U3, T*>::value, "");
}
#endif
}
static void test_sfinae_runtime() {
#if TEST_STD_VER >= 11
{ // the constructor does not participate in overload resolution when
// a base <-> derived conversion would occur.
using UA = std::unique_ptr<A[]>;
using UAD = std::unique_ptr<A[], GenericDeleter>;
using UAC = std::unique_ptr<const A[]>;
using UB = std::unique_ptr<B[]>;
using UBD = std::unique_ptr<B[], GenericDeleter>;
using UBC = std::unique_ptr<const B[]>;
static_assert(!std::is_constructible<UA, B*>::value, "");
static_assert(!std::is_constructible<UB, A*>::value, "");
static_assert(!std::is_constructible<UAD, B*>::value, "");
static_assert(!std::is_constructible<UBD, A*>::value, "");
static_assert(!std::is_constructible<UAC, const B*>::value, "");
static_assert(!std::is_constructible<UBC, const A*>::value, "");
}
#endif
}
DEFINE_AND_RUN_IS_INCOMPLETE_TEST({
{ doIncompleteTypeTest(1, getNewIncomplete()); }
checkNumIncompleteTypeAlive(0);
{
doIncompleteTypeTest<IncompleteType, NCDeleter<IncompleteType> >(
1, getNewIncomplete());
}
checkNumIncompleteTypeAlive(0);
})
int main(int, char**) {
{
test_pointer</*IsArray*/ false>();
test_derived();
test_sfinae<int>();
}
{
test_pointer</*IsArray*/ true>();
test_sfinae<int[]>();
test_sfinae_runtime();
}
return 0;
}
|