]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/shared_ptr/cons/unique_ptr.cc
1 // { dg-do run { target c++11 } }
3 // Copyright (C) 2008-2022 Free Software Foundation, Inc.
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)
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.
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/>.
20 // 20.7.2.2 Class template shared_ptr [util.smartptr.shared]
23 #include <testsuite_hooks.h>
28 struct B
: A
{ ~B() { ++destroyed
; } };
30 // 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
32 // Construction from unique_ptr
34 template<typename From
, typename To
>
35 constexpr bool constructible()
38 return is_constructible
<shared_ptr
<To
>, unique_ptr
<From
>>::value
39 && is_constructible
<shared_ptr
<const To
>, unique_ptr
<From
>>::value
40 && is_constructible
<shared_ptr
<const To
>, unique_ptr
<const From
>>::value
;
43 static_assert( constructible
< A
, A
>(), "A -> A compatible" );
44 static_assert( constructible
< B
, A
>(), "B -> A compatible" );
45 static_assert( constructible
< int, int >(), "int -> int compatible" );
46 static_assert( !constructible
< int, long >(), "int -> long not compatible" );
51 std::unique_ptr
<A
> up(new A
);
52 std::shared_ptr
<A
> sp(std::move(up
));
53 VERIFY( up
.get() == 0 );
54 VERIFY( sp
.get() != 0 );
55 VERIFY( sp
.use_count() == 1 );
61 std::unique_ptr
<B
> b(new B
);
62 std::shared_ptr
<A
> a(std::move(b
));
63 VERIFY( b
.get() == 0 );
64 VERIFY( a
.get() != 0 );
65 VERIFY( a
.use_count() == 1 );
67 VERIFY( destroyed
== 1 );