Commit b2343559 by Jonathan Wakely Committed by Jonathan Wakely

Add array support to std::shared_ptr for C++17

	* doc/xml/manual/status_cxx2017.xml: Update status.
	* doc/html/manual/status.html: Regenerate.
	* include/bits/shared_ptr.h (shared_ptr(unique_ptr<_Yp, _Del>)): Add
	extension constructor to maintain C++14 behaviour.
	* include/bits/shared_ptr_base.h (__sp_array_delete): Add new struct.
	(__shared_count(_Ptr, false_type), __shared_count(_Ptr, true_type)):
	New constructors.
	(__sp_compatible_with, __sp_is_constructible): Add specializations
	for array support.
	(__sp_is_constructible_arr, __sp_is_constructible_arrN): New helpers.
	(__shared_ptr_access): New base class for observer member functions.
	(__shared_ptr::element_type): Use remove_extent.
	(__shared_ptr::_UniqCompatible): Add __sp_compatible_with check.
	(__shared_ptr(_Yp*)): Use tag dispatching to call new __shared_count
	constructor.
	(__shared_ptr(unique_ptr<_Yp, _Del>)): Add extension constructor.
	(__shared_ptr::operator*, __shared_ptr::operator->): Remove and
	inherit from __shared_ptr_access base class.
	(__shared_ptr::__has_esft_base): Return false for array types.
	(__weak_ptr::element_type): Use remove_extent.
	* include/experimental/bits/shared_ptr.h (__libfund_v1): Remove.
	(__shared_ptr<__libfund_v1<_Tp>>): Remove specializations.
	(__wak_ptr<__libfund_v1<_Tp>>): Likewise.
	(experimental::__sp_compatible_v): Redefine using
	__sp_compatible_with.
	(experimental::__sp_is_constructible_v): Redefine using
	__sp_is_constructible.
	(get_deleter, operator<<): Change argument from __shared_ptr to
	shared_ptr.
	* testsuite/20_util/shared_ptr/cons/array.cc: New test.
	* testsuite/20_util/shared_ptr/cons/unique_ptr_array.cc: Adjust for
	new behaviour.
	* testsuite/20_util/shared_ptr/observers/array.cc: Test observers for
	arrays.
	* testsuite/20_util/shared_ptr/observers/array_neg.cc: New test.

From-SVN: r242369
parent a2b4c188
2016-11-13 Jonathan Wakely <jwakely@redhat.com>
* doc/xml/manual/status_cxx2017.xml: Update status.
* doc/html/manual/status.html: Regenerate.
* include/bits/shared_ptr.h (shared_ptr(unique_ptr<_Yp, _Del>)): Add
extension constructor to maintain C++14 behaviour.
* include/bits/shared_ptr_base.h (__sp_array_delete): Add new struct.
(__shared_count(_Ptr, false_type), __shared_count(_Ptr, true_type)):
New constructors.
(__sp_compatible_with, __sp_is_constructible): Add specializations
for array support.
(__sp_is_constructible_arr, __sp_is_constructible_arrN): New helpers.
(__shared_ptr_access): New base class for observer member functions.
(__shared_ptr::element_type): Use remove_extent.
(__shared_ptr::_UniqCompatible): Add __sp_compatible_with check.
(__shared_ptr(_Yp*)): Use tag dispatching to call new __shared_count
constructor.
(__shared_ptr(unique_ptr<_Yp, _Del>)): Add extension constructor.
(__shared_ptr::operator*, __shared_ptr::operator->): Remove and
inherit from __shared_ptr_access base class.
(__shared_ptr::__has_esft_base): Return false for array types.
(__weak_ptr::element_type): Use remove_extent.
* include/experimental/bits/shared_ptr.h (__libfund_v1): Remove.
(__shared_ptr<__libfund_v1<_Tp>>): Remove specializations.
(__wak_ptr<__libfund_v1<_Tp>>): Likewise.
(experimental::__sp_compatible_v): Redefine using
__sp_compatible_with.
(experimental::__sp_is_constructible_v): Redefine using
__sp_is_constructible.
(get_deleter, operator<<): Change argument from __shared_ptr to
shared_ptr.
* testsuite/20_util/shared_ptr/cons/array.cc: New test.
* testsuite/20_util/shared_ptr/cons/unique_ptr_array.cc: Adjust for
new behaviour.
* testsuite/20_util/shared_ptr/observers/array.cc: Test observers for
arrays.
* testsuite/20_util/shared_ptr/observers/array_neg.cc: New test.
2016-11-13 Ville Voutilainen <ville.voutilainen@gmail.com>
Implement P0403R1, Literal suffixes for basic_string_view.
......
......@@ -565,11 +565,11 @@ Feature-testing recommendations for C++</a>.
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> 7 </td><td align="left"> <code class="code">__cpp_lib_apply &gt;= 201603</code> </td></tr><tr bgcolor="#C8B0B0"><td align="left"> Library Fundamentals V1 TS Components: <code class="code">shared_ptr&lt;T[]&gt;</code> </td><td align="left">
</td><td align="center"> 7 </td><td align="left"> <code class="code">__cpp_lib_apply &gt;= 201603</code> </td></tr><tr><td align="left"> Library Fundamentals V1 TS Components: <code class="code">shared_ptr&lt;T[]&gt;</code> </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
</td><td align="center"> No </td><td align="left"> <code class="code">__cpp_lib_shared_ptr_arrays &gt;= 201603</code> </td></tr><tr><td align="left"> Library Fundamentals V1 TS Components: Searchers </td><td align="left">
</td><td align="center"> 7 </td><td align="left"> <code class="code">__cpp_lib_shared_ptr_arrays &gt;= 201603</code> </td></tr><tr><td align="left"> Library Fundamentals V1 TS Components: Searchers </td><td align="left">
<a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html" target="_top">
P0220R1
</a>
......
......@@ -158,14 +158,13 @@ Feature-testing recommendations for C++</link>.
</row>
<row>
<?dbhtml bgcolor="#C8B0B0" ?>
<entry> Library Fundamentals V1 TS Components: <code>shared_ptr&lt;T[]&gt;</code> </entry>
<entry>
<link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0220r1.html">
P0220R1
</link>
</entry>
<entry align="center"> No </entry>
<entry align="center"> 7 </entry>
<entry> <code>__cpp_lib_shared_ptr_arrays >= 201603</code> </entry>
</row>
......
......@@ -277,6 +277,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
shared_ptr(unique_ptr<_Yp, _Del>&& __r)
: __shared_ptr<_Tp>(std::move(__r)) { }
#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
// This non-standard constructor exists to support conversions that
// were possible in C++11 and C++14 but are ill-formed in C++17.
// If an exception is thrown this constructor has no effect.
template<typename _Yp, typename _Del,
_Constructible<unique_ptr<_Yp, _Del>, __sp_array_delete>* = 0>
shared_ptr(unique_ptr<_Yp, _Del>&& __r)
: __shared_ptr<_Tp>(std::move(__r), __sp_array_delete()) { }
#endif
/**
* @brief Construct an empty %shared_ptr.
* @post use_count() == 0 && get() == nullptr
......
......@@ -559,6 +559,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_Impl _M_impl;
};
// The default deleter for shared_ptr<T[]> and shared_ptr<T[N]>.
struct __sp_array_delete
{
template<typename _Yp>
void operator()(_Yp* __p) const { delete[] __p; }
};
template<_Lock_policy _Lp>
class __shared_count
......@@ -582,6 +588,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
}
template<typename _Ptr>
__shared_count(_Ptr __p, /* is_array = */ false_type)
: __shared_count(__p)
{ }
template<typename _Ptr>
__shared_count(_Ptr __p, /* is_array = */ true_type)
: __shared_count(__p, __sp_array_delete{}, allocator<void>())
{ }
template<typename _Ptr, typename _Deleter>
__shared_count(_Ptr __p, _Deleter __d)
: __shared_count(__p, std::move(__d), allocator<void>())
......@@ -848,8 +864,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_pi = nullptr;
}
// Helper traits for shared_ptr
#define __cpp_lib_shared_ptr_arrays 201603
// Helper traits for shared_ptr of array:
// A pointer type Y* is said to be compatible with a pointer type T* when
// either Y* is convertible to T* or Y is U[N] and T is U cv [].
template<typename _Yp_ptr, typename _Tp_ptr>
struct __sp_compatible_with
: false_type
......@@ -860,17 +880,161 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: is_convertible<_Yp*, _Tp*>::type
{ };
template<typename _Up, size_t _Nm>
struct __sp_compatible_with<_Up(*)[_Nm], _Up(*)[]>
: true_type
{ };
template<typename _Up, size_t _Nm>
struct __sp_compatible_with<_Up(*)[_Nm], const _Up(*)[]>
: true_type
{ };
template<typename _Up, size_t _Nm>
struct __sp_compatible_with<_Up(*)[_Nm], volatile _Up(*)[]>
: true_type
{ };
template<typename _Up, size_t _Nm>
struct __sp_compatible_with<_Up(*)[_Nm], const volatile _Up(*)[]>
: true_type
{ };
// Test conversion from Y(*)[N] to U(*)[N] without forming invalid type Y[N].
template<typename _Up, size_t _Nm, typename _Yp, typename = void>
struct __sp_is_constructible_arrN
: false_type
{ };
template<typename _Up, size_t _Nm, typename _Yp>
struct __sp_is_constructible_arrN<_Up, _Nm, _Yp, __void_t<_Yp[_Nm]>>
: is_convertible<_Yp(*)[_Nm], _Up(*)[_Nm]>::type
{ };
// Test conversion from Y(*)[] to U(*)[] without forming invalid type Y[].
template<typename _Up, typename _Yp, typename = void>
struct __sp_is_constructible_arr
: false_type
{ };
template<typename _Up, typename _Yp>
struct __sp_is_constructible_arr<_Up, _Yp, __void_t<_Yp[]>>
: is_convertible<_Yp(*)[], _Up(*)[]>::type
{ };
// Trait to check if shared_ptr<T> can be constructed from Y*.
template<typename _Tp, typename _Yp>
struct __sp_is_constructible;
// When T is U[N], Y(*)[N] shall be convertible to T*;
template<typename _Up, size_t _Nm, typename _Yp>
struct __sp_is_constructible<_Up[_Nm], _Yp>
: __sp_is_constructible_arrN<_Up, _Nm, _Yp>::type
{ };
// when T is U[], Y(*)[] shall be convertible to T*;
template<typename _Up, typename _Yp>
struct __sp_is_constructible<_Up[], _Yp>
: __sp_is_constructible_arr<_Up, _Yp>::type
{ };
// otherwise, Y* shall be convertible to T*.
template<typename _Tp, typename _Yp>
struct __sp_is_constructible
: is_convertible<_Yp*, _Tp*>::type
{ };
// Define operator* and operator-> for shared_ptr<T>.
template<typename _Tp, _Lock_policy _Lp,
bool = is_array<_Tp>::value, bool = is_void<_Tp>::value>
class __shared_ptr_access
{
public:
using element_type = _Tp;
element_type&
operator*() const noexcept
{
__glibcxx_assert(_M_get() != nullptr);
return *_M_get();
}
element_type*
operator->() const noexcept
{
_GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
return _M_get();
}
private:
element_type*
_M_get() const noexcept
{ return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
};
// Define operator-> for shared_ptr<cv void>.
template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr
class __shared_ptr_access<_Tp, _Lp, false, true>
{
public:
using element_type = _Tp;
element_type*
operator->() const noexcept
{
_GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get();
}
};
// Define operator[] for shared_ptr<T[]> and shared_ptr<T[N]>.
template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr_access<_Tp, _Lp, true, false>
{
public:
using element_type = typename remove_extent<_Tp>::type;
#if __cplusplus <= 201402L
[[__deprecated__("shared_ptr<T[]>::operator* is absent from C++17")]]
element_type&
operator*() const noexcept
{
__glibcxx_assert(_M_ptr != nullptr);
return *_M_get();
}
[[__deprecated__("shared_ptr<T[]>::operator-> is absent from C++17")]]
element_type*
operator->() const noexcept
{
_GLIBCXX_DEBUG_PEDASSERT(_M_get() != nullptr);
return _M_get();
}
#endif
element_type&
operator[](ptrdiff_t __i) const
{
__glibcxx_assert(_M_get() != nullptr);
__glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
return _M_get()[__i];
}
private:
// Trait to check if shared_ptr<T> can be constructed from Y*.
template<typename _Tp1, typename _Yp>
using __sp_is_constructible = is_convertible<_Yp*, _Tp1*>;
element_type*
_M_get() const noexcept
{ return static_cast<const __shared_ptr<_Tp, _Lp>*>(this)->get(); }
};
template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr
: public __shared_ptr_access<_Tp, _Lp>
{
public:
using element_type = typename remove_extent<_Tp>::type;
private:
// Constraint for taking ownership of a pointer of type _Yp*:
template<typename _Yp>
using _SafeConv
......@@ -888,9 +1052,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Constraint for construction from unique_ptr:
template<typename _Yp, typename _Del, typename _Res = void,
typename _Ptr = typename unique_ptr<_Yp, _Del>::pointer>
using _UniqCompatible = typename enable_if<
is_convertible<_Ptr, element_type*>::value
, _Res>::type;
using _UniqCompatible = typename enable_if<__and_<
__sp_compatible_with<_Yp*, _Tp*>, is_convertible<_Ptr, element_type*>
>::value, _Res>::type;
// Constraint for assignment from unique_ptr:
template<typename _Yp, typename _Del>
......@@ -909,7 +1073,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Yp, typename = _SafeConv<_Yp>>
explicit
__shared_ptr(_Yp* __p)
: _M_ptr(__p), _M_refcount(__p)
: _M_ptr(__p), _M_refcount(__p, typename is_array<_Tp>::type())
{
static_assert( !is_void<_Yp>::value, "incomplete type" );
static_assert( sizeof(_Yp) > 0, "incomplete type" );
......@@ -995,6 +1159,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_enable_shared_from_this_with(__raw);
}
#if __cplusplus <= 201402L && _GLIBCXX_USE_DEPRECATED
protected:
// If an exception is thrown this constructor has no effect.
template<typename _Tp1, typename _Del,
typename enable_if<__and_<
__not_<is_array<_Tp>>, is_array<_Tp1>,
is_convertible<typename unique_ptr<_Tp1, _Del>::pointer, _Tp*>
>::value, bool>::type = true>
__shared_ptr(unique_ptr<_Tp1, _Del>&& __r, __sp_array_delete)
: _M_ptr(__r.get()), _M_refcount()
{
auto __raw = _S_raw_ptr(__r.get());
_M_refcount = __shared_count<_Lp>(std::move(__r));
_M_enable_shared_from_this_with(__raw);
}
public:
#endif
#if _GLIBCXX_USE_DEPRECATED
// Postcondition: use_count() == 1 and __r.get() == 0
template<typename _Yp, typename = _Compatible<_Yp>>
......@@ -1068,21 +1250,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
reset(_Yp* __p, _Deleter __d, _Alloc __a)
{ __shared_ptr(__p, __d, std::move(__a)).swap(*this); }
// Allow class instantiation when _Tp is [cv-qual] void.
typename std::add_lvalue_reference<element_type>::type
operator*() const noexcept
{
__glibcxx_assert(_M_ptr != 0);
return *_M_ptr;
}
element_type*
operator->() const noexcept
{
_GLIBCXX_DEBUG_PEDASSERT(_M_ptr != 0);
return _M_ptr;
}
element_type*
get() const noexcept
{ return _M_ptr; }
......@@ -1193,7 +1360,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Yp>
struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
: true_type { };
: __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
template<typename _Yp>
typename enable_if<__has_esft_base<_Yp>::value>::type
......@@ -1428,7 +1595,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
using _Assignable = _Compatible<_Yp, __weak_ptr&>;
public:
using element_type = _Tp;
using element_type = typename remove_extent<_Tp>::type;
constexpr __weak_ptr() noexcept
: _M_ptr(nullptr), _M_refcount()
......
// { dg-do run { target c++11 } }
// Copyright (C) 2016 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
// <http://www.gnu.org/licenses/>.
#include <memory>
#include <testsuite_hooks.h>
// C++1z 20.11.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
template<typename To, typename From>
constexpr bool check()
{
using std::shared_ptr;
using std::is_constructible;
return !is_constructible<shared_ptr<To>, shared_ptr<From>>::value
&& !is_constructible<shared_ptr<To>, shared_ptr<From>&>::value;
}
static_assert( check<int, int[]>() );
static_assert( check<int, int[2]>() );
static_assert( check<int[2], void>() );
static_assert( check<int[2], int>() );
static_assert( check<int[2], int[]>() );
static_assert( check<int[], void>() );
static_assert( check<int[], int>() );
int count = 0;
struct A {
A() { ++count; }
~A() { --count; }
};
struct B : A { };
static_assert( check<A, B[2]>() );
static_assert( check<A, B[]>() );
static_assert( check<A[2], B>() );
static_assert( check<A[2], B[2]>() );
static_assert( check<A[2], B[]>() );
static_assert( check<A[], B>() );
static_assert( check<A[], B[2]>() );
static_assert( check<A[], B[]>() );
void
test01()
{
std::shared_ptr<A[2]> p;
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test02()
{
std::shared_ptr<A[]> p;
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test03()
{
std::shared_ptr<A[2]> p(nullptr);
VERIFY( p.get() == nullptr );
VERIFY( p.use_count() == 0 );
p.reset();
VERIFY( count == 0 );
}
void
test04()
{
std::shared_ptr<A[]> 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<A[2]> 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<A[]> 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<A[2]> 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<A[]> p(a);
VERIFY( p.get() == a );
VERIFY( p.use_count() == 1 );
p.reset();
VERIFY( count == 0 );
}
// Converting constrcutor
void
test09()
{
A * const a = new A[2];
std::shared_ptr<A[2]> p(a);
std::shared_ptr<const A[2]> 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<A[]> p(a);
std::shared_ptr<const A[]> 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<A[2]> p(a);
std::shared_ptr<const A[]> 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<A[2]> p(a);
std::shared_ptr<A[2]> 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<A[2]> p(a);
std::shared_ptr<A[]> 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<A[2]> p(a);
std::shared_ptr<A[2]> 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<A[2]> p(a);
std::shared_ptr<A[]> 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();
}
......@@ -22,6 +22,13 @@
#include <memory>
#include <testsuite_hooks.h>
#if __cpp_lib_shared_ptr_arrays >= 201603
# define SHARED_PTR_ARRAYS
#endif
#if __cpp_lib_enable_shared_from_this >= 201603
# define WEAK_FROM_THIS
#endif
int destroyed = 0;
struct A : std::enable_shared_from_this<A>
......@@ -36,12 +43,22 @@ int
test01()
{
std::unique_ptr<A[]> up(new A[2]);
#ifdef SHARED_PTR_ARRAYS
std::shared_ptr<A[]> sp(std::move(up));
#else
std::shared_ptr<A> sp(std::move(up));
#endif
VERIFY( up.get() == 0 );
VERIFY( sp.get() != 0 );
VERIFY( sp.use_count() == 1 );
#ifdef SHARED_PTR_ARRAYS
# ifdef WEAK_FROM_THIS
VERIFY( sp[0].weak_from_this().expired() );
# endif
#else
VERIFY( sp->shared_from_this() != nullptr );
#endif
sp.reset();
VERIFY( destroyed == 2 );
......
// { dg-do run { target c++11 } }
// Copyright (C) 2016 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
// <http://www.gnu.org/licenses/>.
#include <memory>
#include <testsuite_hooks.h>
struct A
{
int i = 0;
};
// C++1z 20.11.2.2.5 shared_ptr observers [util.smartptr.shared.obs]
// get
void
test01()
{
A * const a = new A[2];
const std::shared_ptr<A[2]> p(a);
VERIFY( p.get() == a );
}
// get
void
test02()
{
A * const a = new A[2];
const std::shared_ptr<A[]> p(a);
VERIFY( p.get() == a );
}
// operator[]
void
test03()
{
A * const a = new A[2];
const std::shared_ptr<A[2]> p(a);
VERIFY( &p[0] == a );
}
// operator[]
void
test04()
{
A * const a = new A[2];
const std::shared_ptr<A[]> p(a);
VERIFY( &p[0] == a );
}
int
main()
{
test01();
test02();
test03();
test04();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment