Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/casts/reinterpret.cc

raw
1 // { dg-do run { target c++17 } }
2 // { dg-require-effective-target hosted }
3
4 // Copyright (C) 2016-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.11.2.2.9 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::reinterpret_pointer_cast;
36
37 shared_ptr<double> spd;
38 shared_ptr<const int> spci;
39 shared_ptr<MyP> spa;
40
41 check_ret_type<shared_ptr<void>>(reinterpret_pointer_cast<void>(spd));
42 check_ret_type<shared_ptr<const short>>(reinterpret_pointer_cast<const short>(spci));
43 check_ret_type<shared_ptr<MyDP>>(reinterpret_pointer_cast<MyDP>(spa));
44 }
45
46 void
47 test02()
48 {
49 using std::shared_ptr;
50 using std::reinterpret_pointer_cast;
51
52 int* ptr = new int(2);
53 shared_ptr<int> pi(ptr);
54 auto pl = reinterpret_pointer_cast<long>(pi);
55 VERIFY(pi.use_count() == 2);
56 VERIFY(pl.use_count() == 2);
57 VERIFY(pi.get() == ptr);
58 VERIFY(reinterpret_cast<int*>(pl.get()) == ptr);
59 }
60
61 int main()
62 {
63 test01();
64 test02();
65 }