| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-require-effective-target hosted } |
| 3 |
|
| 4 |
// Copyright (C) 2006-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 |
// 20.6.6.2.10 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 |
| 31 |
test01() |
| 32 |
{ |
| 33 |
using __gnu_test::check_ret_type; |
| 34 |
using std::shared_ptr; |
| 35 |
using std::static_pointer_cast; |
| 36 |
using std::const_pointer_cast; |
| 37 |
using std::dynamic_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>(spd)); |
| 44 |
check_ret_type<shared_ptr<int>>(const_pointer_cast<int>(spci)); |
| 45 |
// Non-polymorphic dynamic_cast works without RTTI. |
| 46 |
check_ret_type<shared_ptr<MyP>>(dynamic_pointer_cast<MyP>(spa)); |
| 47 |
#if __cpp_rtti |
| 48 |
// But polymorphic dynamic_cast needs RTTI. |
| 49 |
check_ret_type<shared_ptr<MyDP>>(dynamic_pointer_cast<MyDP>(spa)); |
| 50 |
#endif |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
test02() |
| 55 |
{ |
| 56 |
using std::shared_ptr; |
| 57 |
using std::static_pointer_cast; |
| 58 |
using std::const_pointer_cast; |
| 59 |
using std::dynamic_pointer_cast; |
| 60 |
|
| 61 |
int* ptr = new int(1); |
| 62 |
shared_ptr<const void> pcv(ptr); |
| 63 |
auto pci = static_pointer_cast<const int>(pcv); |
| 64 |
VERIFY(pci.use_count() == 2); |
| 65 |
VERIFY(pcv.use_count() == 2); |
| 66 |
VERIFY(pci.get() == ptr); |
| 67 |
VERIFY(pcv.get() == ptr); |
| 68 |
auto pi = const_pointer_cast<int>(pci); |
| 69 |
VERIFY(pi.use_count() == 3); |
| 70 |
VERIFY(pcv.use_count() == 3); |
| 71 |
VERIFY(pi.get() == ptr); |
| 72 |
VERIFY(pci.get() == ptr); |
| 73 |
|
| 74 |
#if __cpp_rtti |
| 75 |
MyP* pptr = new MyP; |
| 76 |
shared_ptr<MyP> pp(pptr); |
| 77 |
auto pdp = dynamic_pointer_cast<MyDP>(pp); |
| 78 |
VERIFY(pp.use_count() == 1); |
| 79 |
VERIFY(pdp.use_count() == 0); |
| 80 |
VERIFY(pdp.get() == nullptr); |
| 81 |
VERIFY(pp.get() == pptr); |
| 82 |
pptr = new MyDP; |
| 83 |
pp.reset(pptr); |
| 84 |
pdp = dynamic_pointer_cast<MyDP>(pp); |
| 85 |
VERIFY(pp.use_count() == 2); |
| 86 |
VERIFY(pdp.use_count() == 2); |
| 87 |
VERIFY(pdp.get() == pptr); |
| 88 |
VERIFY(pp.get() == pptr); |
| 89 |
#endif |
| 90 |
} |
| 91 |
|
| 92 |
int main() |
| 93 |
{ |
| 94 |
test01(); |
| 95 |
test02(); |
| 96 |
} |