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

source code of libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cast/const_pointer_cast.pass.cpp