// { dg-do run { target c++11 } }
// { dg-require-effective-target hosted }
// Copyright (C) 2016-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3. If not see
// .
#include
#include
// C++17 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
template
constexpr bool check()
{
using std::shared_ptr;
using std::is_constructible;
return !is_constructible, shared_ptr>::value
&& !is_constructible, shared_ptr&>::value;
}
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
int count = 0;
struct A {
A() { ++count; }
~A() { --count; }
};
struct B : A { };
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
static_assert( check() );
void
test01()
{
std::shared_ptr p;
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test02()
{
std::shared_ptr p;
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test03()
{
std::shared_ptr p(nullptr);
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test04()
{
std::shared_ptr p(nullptr);
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
// Construction from pointer
void
test05()
{
A * const a = nullptr;
std::shared_ptr p(a);
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 1 );
p.reset();
VERIFY( count == 0 );
}
void
test06()
{
A * const a = nullptr;
std::shared_ptr p(a);
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 1 );
p.reset();
VERIFY( count == 0 );
}
void
test07()
{
A * const a = new A[2];
std::shared_ptr p(a);
VERIFY( p.get() == a );
VERIFY( p.use_count() == 1 );
p.reset();
VERIFY( count == 0 );
}
void
test08()
{
A * const a = new A[2];
std::shared_ptr p(a);
VERIFY( p.get() == a );
VERIFY( p.use_count() == 1 );
p.reset();
VERIFY( count == 0 );
}
// Converting constructor
void
test09()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(p);
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 2 );
VERIFY( p2.use_count() == 2 );
p.reset();
VERIFY( count != 0 );
p2.reset();
VERIFY( count == 0 );
}
void
test10()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(p);
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 2 );
VERIFY( p2.use_count() == 2 );
p.reset();
VERIFY( count != 0 );
p2.reset();
VERIFY( count == 0 );
}
void
test11()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(p);
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 2 );
VERIFY( p2.use_count() == 2 );
p.reset();
VERIFY( count != 0 );
p2.reset();
VERIFY( count == 0 );
}
// Copy construction
void
test12()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(p);
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 2 );
VERIFY( p2.use_count() == 2 );
p.reset();
VERIFY( count != 0 );
p2.reset();
VERIFY( count == 0 );
}
void
test13()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(p);
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 2 );
VERIFY( p2.use_count() == 2 );
p.reset();
VERIFY( count != 0 );
p2.reset();
VERIFY( count == 0 );
}
// Move construction
void
test14()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(std::move(p));
VERIFY( p.get() == nullptr );
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 0 );
VERIFY( p2.use_count() == 1 );
p2.reset();
VERIFY( count == 0 );
}
void
test15()
{
A * const a = new A[2];
std::shared_ptr p(a);
std::shared_ptr p2(std::move(p));
VERIFY( p.get() == nullptr );
VERIFY( p2.get() == a );
VERIFY( p.use_count() == 0 );
VERIFY( p2.use_count() == 1 );
p2.reset();
VERIFY( count == 0 );
}
int
main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
test08();
test09();
test10();
test11();
test12();
test13();
test14();
test15();
}