110 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// template<class Y> explicit shared_ptr(Y* p);12 13#include <cassert>14#include <memory>15#include <type_traits>16 17#include "test_macros.h"18 19struct A20{21 static int count;22 23 A() {++count;}24 A(const A&) {++count;}25 ~A() {--count;}26};27 28int A::count = 0;29 30struct derived : A {};31 32// https://llvm.org/PR6025833// Invalid constructor SFINAE for std::shared_ptr's array ctors34static_assert( std::is_constructible<std::shared_ptr<int>, int*>::value, "");35static_assert( std::is_constructible<std::shared_ptr<A>, derived*>::value, "");36static_assert(!std::is_constructible<std::shared_ptr<A>, int*>::value, "");37 38#if TEST_STD_VER >= 1739static_assert( std::is_constructible<std::shared_ptr<int[]>, int*>::value, "");40static_assert(!std::is_constructible<std::shared_ptr<int[]>, int(*)[]>::value, "");41static_assert( std::is_constructible<std::shared_ptr<int[5]>, int*>::value, "");42static_assert(!std::is_constructible<std::shared_ptr<int[5]>, int(*)[5]>::value, "");43#endif44 45// Test explicit46static_assert(std::is_constructible<std::shared_ptr<int>, int*>::value, "");47static_assert(!std::is_convertible<int*, std::shared_ptr<int> >::value, "");48 49int main(int, char**)50{51 {52 assert(A::count == 0);53 A* ptr = new A;54 std::shared_ptr<A> p(ptr);55 assert(A::count == 1);56 assert(p.use_count() == 1);57 assert(p.get() == ptr);58 }59 60 {61 assert(A::count == 0);62 A const* ptr = new A;63 std::shared_ptr<A const> p(ptr);64 assert(A::count == 1);65 assert(p.use_count() == 1);66 assert(p.get() == ptr);67 }68 69 {70 assert(A::count == 0);71 A* ptr = new A;72 std::shared_ptr<void> p(ptr);73 assert(A::count == 1);74 assert(p.use_count() == 1);75 assert(p.get() == ptr);76 }77 78#if TEST_STD_VER > 1479 {80 assert(A::count == 0);81 std::shared_ptr<A[8]> pA(new A[8]);82 assert(pA.use_count() == 1);83 assert(A::count == 8);84 }85 86 {87 assert(A::count == 0);88 std::shared_ptr<A[]> pA(new A[8]);89 assert(pA.use_count() == 1);90 assert(A::count == 8);91 }92 93 {94 assert(A::count == 0);95 std::shared_ptr<const A[]> pA(new A[8]);96 assert(pA.use_count() == 1);97 assert(A::count == 8);98 }99 100 {101 assert(A::count == 0);102 std::shared_ptr<A> pA(new derived);103 assert(pA.use_count() == 1);104 assert(A::count == 1);105 }106#endif107 108 return 0;109}110