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 // shared_ptr 12 13 // template<class T, class U> shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r); 14 15 #include <memory> 16 #include <type_traits> 17 #include <cassert> 18 19 #include "test_macros.h" 20 21 struct B 22 { 23 static int count; 24 BB25 B() {++count;} BB26 B(const B&) {++count;} ~BB27 virtual ~B() {--count;} 28 }; 29 30 int B::count = 0; 31 32 struct A 33 : public B 34 { 35 static int count; 36 AA37 A() {++count;} AA38 A(const A& other) : B(other) {++count;} ~AA39 ~A() {--count;} 40 }; 41 42 int A::count = 0; 43 main(int,char **)44int main(int, char**) 45 { 46 { 47 const std::shared_ptr<A> pA(new A); 48 std::shared_ptr<B> pB = std::static_pointer_cast<B>(pA); 49 assert(pB.get() == pA.get()); 50 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 51 } 52 { 53 const std::shared_ptr<B> pA(new A); 54 std::shared_ptr<A> pB = std::static_pointer_cast<A>(pA); 55 assert(pB.get() == pA.get()); 56 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 57 } 58 { 59 const std::shared_ptr<A> pA; 60 std::shared_ptr<B> pB = std::static_pointer_cast<B>(pA); 61 assert(pB.get() == pA.get()); 62 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 63 } 64 { 65 const std::shared_ptr<B> pA; 66 std::shared_ptr<A> pB = std::static_pointer_cast<A>(pA); 67 assert(pB.get() == pA.get()); 68 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 69 } 70 #if TEST_STD_VER > 14 71 { 72 const std::shared_ptr<A[8]> pA; 73 std::shared_ptr<B[8]> pB = std::static_pointer_cast<B[8]>(pA); 74 assert(pB.get() == pA.get()); 75 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 76 } 77 { 78 const std::shared_ptr<B[8]> pA; 79 std::shared_ptr<A[8]> pB = std::static_pointer_cast<A[8]>(pA); 80 assert(pB.get() == pA.get()); 81 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 82 } 83 #endif // TEST_STD_VER > 14 84 85 return 0; 86 } 87