Debian Codegraph @

libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc

raw
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2015-2025 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 // 8.2.1 Class template shared_ptr [memory.smartptr.shared]
21
22 #include <experimental/memory>
23 #include <testsuite_hooks.h>
24
25 struct A { };
26 struct B : A { };
27
28 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
29
30 template<typename From, typename To>
31 constexpr bool constructible()
32 {
33 using std::experimental::shared_ptr;
34 using std::experimental::weak_ptr;
35 using std::experimental::is_constructible_v;
36 return is_constructible_v<shared_ptr<To>, weak_ptr<From>>
37 && is_constructible_v<shared_ptr<To>, const weak_ptr<From>&>;
38 }
39
40 static_assert( constructible< A, A >(), "A -> A compatible" );
41 static_assert( !constructible< A, A[] >(), "A -> A[] not compatible" );
42 static_assert( !constructible< A, A[1] >(), "A -> A[1] not compatible" );
43 static_assert( !constructible< A[], A >(), "A[] -> A not compatible" );
44 static_assert( constructible< A[], A[] >(), "A[] -> A[] compatible" );
45 static_assert( !constructible< A[], A[1] >(), "A[] -> A[1] not compatible" );
46 static_assert( !constructible< A[1], A >(), "A[1] -> A not compatible" );
47 static_assert( constructible< A[1], A[] >(), "A[1] -> A[] compatible" );
48 static_assert( constructible< A[1], A[1] >(), "A[1] -> A[1] compatible" );
49 static_assert( !constructible< A[2], A[1] >(), "A[2] -> A[1] not compatible" );
50
51 static_assert( constructible< B, A >(), "B -> A compatible" );
52 static_assert( !constructible< B, A[] >(), "B -> A[] not compatible" );
53 static_assert( !constructible< B, A[1] >(), "B -> A[1] not compatible" );
54 static_assert( !constructible< B[], A >(), "B[] -> A not compatible" );
55 static_assert( !constructible< B[], A[] >(), "B[] -> A[] not compatible" );
56 static_assert( !constructible< B[], A[1] >(), "B[] -> A[1] not compatible" );
57 static_assert( !constructible< B[1], A >(), "B[] -> A not compatible" );
58 static_assert( !constructible< B[1], A[] >(), "B[] -> A[] not compatible" );
59 static_assert( !constructible< B[1], A[1] >(), "B[] -> A[1] not compatible" );
60 static_assert( !constructible< B[2], A[1] >(), "B[2] -> A[1] not compatible" );
61
62
63
64 // Construction from weak_ptr
65 void
66 test01()
67 {
68 A * a = new A[5];
69 std::experimental::shared_ptr<A[5]> a1(a);
70 std::experimental::weak_ptr<A[5]> wa(a1);
71 std::experimental::shared_ptr<A[5]> a2(wa);
72 std::experimental::shared_ptr<A[5]> a3 = wa.lock();
73 VERIFY( a2.get() == a );
74 VERIFY( a3.get() == a );
75 VERIFY( a2.use_count() == wa.use_count() );
76 VERIFY( a3.use_count() == wa.use_count() );
77 }
78
79 int
80 main()
81 {
82 test01();
83 }