Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/creation/dr925.cc

raw
1 // { dg-options "-Wno-deprecated" }
2 // { dg-add-options using-deprecated }
3 // { dg-do run { target c++11 } }
4 // { dg-require-effective-target hosted }
5
6 // Copyright (C) 2010-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // 20.9.11.2 Template class shared_ptr [util.smartptr.shared]
24
25 #include <memory>
26 #include <testsuite_hooks.h>
27
28 struct A
29 {
30 };
31
32 std::unique_ptr<A>
33 create_unique_ptr()
34 {
35 return std::unique_ptr<A>(new A());
36 }
37
38 std::auto_ptr<A>
39 create_auto_ptr()
40 {
41 return std::auto_ptr<A>(new A());
42 }
43
44 void
45 process(std::shared_ptr<A> a)
46 {
47 VERIFY( a.get() != 0 );
48 VERIFY( a.use_count() == 1 );
49 }
50
51 // 20.9.11.2.1 shared_ptr creation [util.smartptr.shared.const]
52
53 // Implicit conversion of auto_ptr to shared_ptr is allowed
54
55 void
56 test01()
57 {
58 process(create_auto_ptr());
59 }
60
61 void
62 test02()
63 {
64 std::auto_ptr<A> a = create_auto_ptr();
65 process(std::move(a));
66 }
67
68 // Implicit conversion of unique_ptr to shared_ptr is allowed
69
70 void
71 test03()
72 {
73 process(create_unique_ptr());
74 }
75
76 void
77 test04()
78 {
79 std::unique_ptr<A> a = create_unique_ptr();
80 process(std::move(a));
81 }
82
83 int
84 main()
85 {
86 test01();
87 test02();
88 test03();
89 test04();
90 return 0;
91 }