Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/assign/auto_ptr.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) 2005-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.6.6.2 Template class shared_ptr [util.smartptr.shared]
24
25 #include <memory>
26 #include <testsuite_hooks.h>
27
28 struct A
29 {
30 A() { ++ctor_count; }
31 virtual ~A() { ++dtor_count; }
32 static long ctor_count;
33 static long dtor_count;
34 };
35 long A::ctor_count = 0;
36 long A::dtor_count = 0;
37
38 struct B : A
39 {
40 B() { ++ctor_count; }
41 virtual ~B() { ++dtor_count; }
42 static long ctor_count;
43 static long dtor_count;
44 };
45 long B::ctor_count = 0;
46 long B::dtor_count = 0;
47
48
49 struct reset_count_struct
50 {
51 ~reset_count_struct()
52 {
53 A::ctor_count = 0;
54 A::dtor_count = 0;
55 B::ctor_count = 0;
56 B::dtor_count = 0;
57 }
58 };
59
60
61 // 20.6.6.2.3 shared_ptr assignment [util.smartptr.shared.assign]
62
63 // Assignment from auto_ptr<Y>
64 int
65 test01()
66 {
67 reset_count_struct __attribute__((unused)) reset;
68
69 std::shared_ptr<A> a(new A);
70 std::auto_ptr<B> b(new B);
71 a = std::move(b);
72 VERIFY( a.get() != 0 );
73 VERIFY( b.get() == 0 );
74 VERIFY( A::ctor_count == 2 );
75 VERIFY( A::dtor_count == 1 );
76 VERIFY( B::ctor_count == 1 );
77 VERIFY( B::dtor_count == 0 );
78
79 return 0;
80 }
81
82 int
83 main()
84 {
85 test01();
86 return 0;
87 }