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> dynamic_pointer_cast(const shared_ptr<U>& r);
14 
15 // UNSUPPORTED: no-rtti
16 
17 #include <memory>
18 #include <type_traits>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 
23 struct B
24 {
25     static int count;
26 
BB27     B() {++count;}
BB28     B(const B&) {++count;}
~BB29     virtual ~B() {--count;}
30 };
31 
32 int B::count = 0;
33 
34 struct A
35     : public B
36 {
37     static int count;
38 
AA39     A() {++count;}
AA40     A(const A& other) : B(other) {++count;}
~AA41     ~A() {--count;}
42 };
43 
44 int A::count = 0;
45 
main(int,char **)46 int main(int, char**)
47 {
48     {
49         const std::shared_ptr<B> pB(new A);
50         std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(pB);
51         assert(pA.get() == pB.get());
52         assert(!pB.owner_before(pA) && !pA.owner_before(pB));
53     }
54     {
55         const std::shared_ptr<B> pB(new B);
56         std::shared_ptr<A> pA = std::dynamic_pointer_cast<A>(pB);
57         assert(pA.get() == 0);
58         assert(pA.use_count() == 0);
59     }
60 #if TEST_STD_VER > 14
61     {
62       const std::shared_ptr<B[8]> pB(new B[8]);
63       std::shared_ptr<A[8]> pA = std::dynamic_pointer_cast<A[8]>(pB);
64       assert(pA.get() == 0);
65       assert(pA.use_count() == 0);
66     }
67 #endif // TEST_STD_VER > 14
68 
69     return 0;
70 }
71