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> const_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<const A> pA(new A); 48 std::shared_ptr<A> pB = std::const_pointer_cast<A>(pA); 49 assert(pB.get() == pA.get()); 50 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 51 } 52 { 53 const std::shared_ptr<const A> pA; 54 std::shared_ptr<A> pB = std::const_pointer_cast<A>(pA); 55 assert(pB.get() == pA.get()); 56 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 57 } 58 #if TEST_STD_VER > 14 59 { 60 const std::shared_ptr<const A[8]> pA; 61 std::shared_ptr<A[8]> pB = std::const_pointer_cast<A[8]>(pA); 62 assert(pB.get() == pA.get()); 63 assert(!pB.owner_before(pA) && !pA.owner_before(pB)); 64 } 65 #endif // TEST_STD_VER > 14 66 67 return 0; 68 } 69