]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/unique_ptr_array.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / shared_ptr / cons / unique_ptr_array.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2012-2020 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // 20.7.2.2 Class template shared_ptr [util.smartptr.shared]
21
22 #include <memory>
23 #include <testsuite_hooks.h>
24
25 #if __cpp_lib_shared_ptr_arrays >= 201603
26 # define SHARED_PTR_ARRAYS
27 #endif
28 #if __cpp_lib_enable_shared_from_this >= 201603
29 # define WEAK_FROM_THIS
30 #endif
31
32 int destroyed = 0;
33
34 struct A : std::enable_shared_from_this<A>
35 {
36 ~A() { ++destroyed; }
37 };
38
39 // 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
40
41 // Construction from unique_ptr<A[]>
42 int
43 test01()
44 {
45 std::unique_ptr<A[]> up(new A[2]);
46 #ifdef SHARED_PTR_ARRAYS
47 std::shared_ptr<A[]> sp(std::move(up));
48 #else
49 std::shared_ptr<A> sp(std::move(up));
50 #endif
51 VERIFY( up.get() == 0 );
52 VERIFY( sp.get() != 0 );
53 VERIFY( sp.use_count() == 1 );
54
55 #ifdef SHARED_PTR_ARRAYS
56 # ifdef WEAK_FROM_THIS
57 VERIFY( sp[0].weak_from_this().expired() );
58 # endif
59 #else
60 VERIFY( sp->shared_from_this() != nullptr );
61 #endif
62
63 sp.reset();
64 VERIFY( destroyed == 2 );
65
66 return 0;
67 }
68
69 int
70 main()
71 {
72 test01();
73 return 0;
74 }