]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/shared_ptr/casts/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / casts / 1.cc
CommitLineData
fb3fc4bd 1// { dg-do run { target c++11 } }
7cc9022f 2// { dg-require-effective-target hosted }
aaf0ca6f 3
a945c346 4// Copyright (C) 2006-2024 Free Software Foundation, Inc.
aaf0ca6f
JW
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
748086b7 9// Free Software Foundation; either version 3, or (at your option)
aaf0ca6f
JW
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
748086b7
JJ
18// with this library; see the file COPYING3. If not see
19// <http://www.gnu.org/licenses/>.
aaf0ca6f
JW
20
21// 20.6.6.2.10 shared_ptr casts [util.smartptr.shared.cast]
22
23#include <memory>
fb3fc4bd 24#include <testsuite_hooks.h>
aaf0ca6f
JW
25#include <testsuite_tr1.h>
26
aaf0ca6f
JW
27struct MyP { virtual ~MyP() { }; };
28struct MyDP : MyP { };
29
fb3fc4bd
JW
30void
31test01()
aaf0ca6f
JW
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
fb3fc4bd
JW
43 check_ret_type<shared_ptr<void>>(static_pointer_cast<void>(spd));
44 check_ret_type<shared_ptr<int>>(const_pointer_cast<int>(spci));
b6b66006
JW
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.
fb3fc4bd 49 check_ret_type<shared_ptr<MyDP>>(dynamic_pointer_cast<MyDP>(spa));
b6b66006 50#endif
fb3fc4bd
JW
51}
52
53void
54test02()
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
b6b66006 74#if __cpp_rtti
fb3fc4bd
JW
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);
b6b66006 89#endif
fb3fc4bd
JW
90}
91
92int main()
93{
94 test01();
95 test02();
aaf0ca6f 96}