| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // shared_ptr casts [util.smartptr.shared.cast] |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <testsuite_hooks.h> |
| 25 | #include <testsuite_tr1.h> |
| 26 | |
| 27 | struct MyP { virtual ~MyP() { }; }; |
| 28 | struct MyDP : MyP { }; |
| 29 | |
| 30 | void test01() |
| 31 | { |
| 32 | using __gnu_test::check_ret_type; |
| 33 | using std::shared_ptr; |
| 34 | using std::static_pointer_cast; |
| 35 | using std::const_pointer_cast; |
| 36 | using std::dynamic_pointer_cast; |
| 37 | using std::reinterpret_pointer_cast; |
| 38 | |
| 39 | shared_ptr<double> spd; |
| 40 | shared_ptr<const int> spci; |
| 41 | shared_ptr<MyP> spa; |
| 42 | |
| 43 | check_ret_type<shared_ptr<void>>(static_pointer_cast<void>(std::move(spd))); |
| 44 | check_ret_type<shared_ptr<int>>(const_pointer_cast<int>(std::move(spci))); |
| 45 | check_ret_type<shared_ptr<MyP>>(dynamic_pointer_cast<MyP>(std::move(spa))); |
| 46 | #if __cpp_rtti |
| 47 | check_ret_type<shared_ptr<MyDP>>(dynamic_pointer_cast<MyDP>(std::move(spa))); |
| 48 | #endif |
| 49 | check_ret_type<shared_ptr<void>>(reinterpret_pointer_cast<void>(std::move(spd))); |
| 50 | check_ret_type<shared_ptr<const short>>(reinterpret_pointer_cast<const short>(std::move(spci))); |
| 51 | check_ret_type<shared_ptr<MyDP>>(reinterpret_pointer_cast<MyDP>(std::move(spa))); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | test02() |
| 56 | { |
| 57 | using std::shared_ptr; |
| 58 | using std::static_pointer_cast; |
| 59 | using std::const_pointer_cast; |
| 60 | using std::dynamic_pointer_cast; |
| 61 | using std::reinterpret_pointer_cast; |
| 62 | |
| 63 | int* ptr = new int(1); |
| 64 | shared_ptr<const void> pcv(ptr); |
| 65 | auto pci = static_pointer_cast<const int>(std::move(pcv)); |
| 66 | VERIFY(pci.use_count() == 1); |
| 67 | VERIFY(pcv.use_count() == 0); |
| 68 | VERIFY(pci.get() == ptr); |
| 69 | VERIFY(pcv.get() == nullptr); |
| 70 | auto pi = const_pointer_cast<int>(std::move(pci)); |
| 71 | VERIFY(pi.use_count() == 1); |
| 72 | VERIFY(pci.use_count() == 0); |
| 73 | VERIFY(pi.get() == ptr); |
| 74 | VERIFY(pci.get() == nullptr); |
| 75 | |
| 76 | #if __cpp_rtti |
| 77 | MyP* pptr = new MyP; |
| 78 | shared_ptr<MyP> pp(pptr); |
| 79 | auto pdp = dynamic_pointer_cast<MyDP>(std::move(pp)); |
| 80 | VERIFY(pdp.use_count() == 0); |
| 81 | VERIFY(pp.use_count() == 1); |
| 82 | VERIFY(pdp.get() == nullptr); |
| 83 | VERIFY(pp.get() == pptr); |
| 84 | pptr = new MyDP; |
| 85 | pp.reset(pptr); |
| 86 | pdp = dynamic_pointer_cast<MyDP>(std::move(pp)); |
| 87 | VERIFY(pdp.use_count() == 1); |
| 88 | VERIFY(pp.use_count() == 0); |
| 89 | VERIFY(pdp.get() == pptr); |
| 90 | VERIFY(pp.get() == nullptr); |
| 91 | |
| 92 | ptr = new int(2); |
| 93 | pi.reset(ptr); |
| 94 | auto pl = reinterpret_pointer_cast<long>(std::move(pi)); |
| 95 | VERIFY(pl.use_count() == 1); |
| 96 | VERIFY(pi.use_count() == 0); |
| 97 | VERIFY(reinterpret_cast<int*>(pl.get()) == ptr); |
| 98 | VERIFY(pi.get() == nullptr); |
| 99 | #endif |
| 100 | } |
| 101 | |
| 102 | int main() |
| 103 | { |
| 104 | test01(); |
| 105 | test02(); |
| 106 | } |