Commit a2e0054e by Jonathan Wakely Committed by Jonathan Wakely

Prepare shared_ptr for array support

	* include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&))
	(shared_ptr(auto_ptr&&)): Adjust template parameter lists.
	* include/bits/shared_ptr.h (__sp_compatible_with)
	(__sp_is_constructible): New helper traits for shared_ptr.
	(shared_ptr::_Convertible): Replace with _Constructible.
	(shared_ptr::_Constructible, shared_ptr::_Assignable): Forward checks
	to base class.
	(shared_ptr::shared_ptr, shared_ptr::operator=): Constrain template
	with _Constructible and _Assignable.
	(shared_ptr::shared_ptr(shared_ptr<_Tp1>, _Tp*)): Use element_type
	instead of _Tp.
	(operator<): Likewise.
	(operator>): Define in terms of operator<.
	(static_pointer_cast, const_pointer_cast, dynamic_pointer_cast): Use
	element_type instead of _Tp.
	(reinterpret_pointer_cast): Define for C++17.
	(weak_ptr::_Convertible): Replace with _Constructible.
	(weak_ptr::_Constructible, weak_ptr::_Assignable): Forward checks
	to base class.
	(weak_ptr::weak_ptr, weak_ptr::operator=): Constrain templates
	with _Constructible and _Assignable.
	* include/bits/shared_ptr_base.h (__shared_ptr::_Convertible): Replace
	with _Compatible.
	(__shared_ptr::_SafeConv): New constraint for incoming raw pointers.
	(__shared_ptr::_Compatible): New constraint for converting from
	other types of shared_ptr and weak_ptr.
	(__shared_ptr::_Assignable): Define in terms of _Compatible.
	(__shared_ptr::_UniqCompatible, __shared_ptr::_UniqAssignable): New
	constraints for converting from unique_ptr.
	(__shared_ptr::__shared_ptr, __shared_ptr::operator=): Constrain
	template with _SaveConf, _Compatible and _Assignable. Remove
	__glibcxx_function_requires concept checks. Add static assertion for
	deleter expression being well-formed.
	(__shared_ptr::__shared_ptr(__shared_ptr<_Tp1>, _Tp*))
	(__shared_ptr::operator*, __shared_ptr::operator->)
	(__shared_ptr::get, __shared_ptr::_M_ptr): Use element_type instead
	of _Tp.
	(operator<): Likewise.
	(operator>): Define in terms of operator<.
	(static_pointer_cast, const_pointer_cast, dynamic_pointer_cast): Use
	element_type instead of _Tp.
	(reinterpret_pointer_cast): Define for C++17.
	(weak_ptr::_Convertible): Replace with _Compatible.
	(weak_ptr::_Compatible, weak_ptr::_Assignable): New constraints for
	conversions from other types of weak_ptr and shared_ptr.
	(__weak_ptr::__weak_ptr, __weak_ptr::operator=): Constrain templates
	with _Constructible and _Assignable.
	(__weak_ptr::_M_ptr): Use element_type instead of _Tp.
	* testsuite/20_util/shared_ptr/assign/auto_ptr_neg.cc: Adjust
	dg-error pattern.
	* testsuite/20_util/shared_ptr/cons/auto_ptr.cc: Test conversions.
	* testsuite/20_util/shared_ptr/cons/unique_ptr.cc: Likewise.
	* testsuite/20_util/shared_ptr/cons/void_neg.cc: Likewise.
	* testsuite/20_util/shared_ptr/casts/reinterpret.cc: New test.

From-SVN: r241373
parent f320e6a0
2016-10-20 Jonathan Wakely <jwakely@redhat.com>
* include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&))
(shared_ptr(auto_ptr&&)): Adjust template parameter lists.
* include/bits/shared_ptr.h (__sp_compatible_with)
(__sp_is_constructible): New helper traits for shared_ptr.
(shared_ptr::_Convertible): Replace with _Constructible.
(shared_ptr::_Constructible, shared_ptr::_Assignable): Forward checks
to base class.
(shared_ptr::shared_ptr, shared_ptr::operator=): Constrain template
with _Constructible and _Assignable.
(shared_ptr::shared_ptr(shared_ptr<_Tp1>, _Tp*)): Use element_type
instead of _Tp.
(operator<): Likewise.
(operator>): Define in terms of operator<.
(static_pointer_cast, const_pointer_cast, dynamic_pointer_cast): Use
element_type instead of _Tp.
(reinterpret_pointer_cast): Define for C++17.
(weak_ptr::_Convertible): Replace with _Constructible.
(weak_ptr::_Constructible, weak_ptr::_Assignable): Forward checks
to base class.
(weak_ptr::weak_ptr, weak_ptr::operator=): Constrain templates
with _Constructible and _Assignable.
* include/bits/shared_ptr_base.h (__shared_ptr::_Convertible): Replace
with _Compatible.
(__shared_ptr::_SafeConv): New constraint for incoming raw pointers.
(__shared_ptr::_Compatible): New constraint for converting from
other types of shared_ptr and weak_ptr.
(__shared_ptr::_Assignable): Define in terms of _Compatible.
(__shared_ptr::_UniqCompatible, __shared_ptr::_UniqAssignable): New
constraints for converting from unique_ptr.
(__shared_ptr::__shared_ptr, __shared_ptr::operator=): Constrain
template with _SaveConf, _Compatible and _Assignable. Remove
__glibcxx_function_requires concept checks. Add static assertion for
deleter expression being well-formed.
(__shared_ptr::__shared_ptr(__shared_ptr<_Tp1>, _Tp*))
(__shared_ptr::operator*, __shared_ptr::operator->)
(__shared_ptr::get, __shared_ptr::_M_ptr): Use element_type instead
of _Tp.
(operator<): Likewise.
(operator>): Define in terms of operator<.
(static_pointer_cast, const_pointer_cast, dynamic_pointer_cast): Use
element_type instead of _Tp.
(reinterpret_pointer_cast): Define for C++17.
(weak_ptr::_Convertible): Replace with _Compatible.
(weak_ptr::_Compatible, weak_ptr::_Assignable): New constraints for
conversions from other types of weak_ptr and shared_ptr.
(__weak_ptr::__weak_ptr, __weak_ptr::operator=): Constrain templates
with _Constructible and _Assignable.
(__weak_ptr::_M_ptr): Use element_type instead of _Tp.
* testsuite/20_util/shared_ptr/assign/auto_ptr_neg.cc: Adjust
dg-error pattern.
* testsuite/20_util/shared_ptr/cons/auto_ptr.cc: Test conversions.
* testsuite/20_util/shared_ptr/cons/unique_ptr.cc: Likewise.
* testsuite/20_util/shared_ptr/cons/void_neg.cc: Likewise.
* testsuite/20_util/shared_ptr/casts/reinterpret.cc: New test.
2016-10-20 Ville Voutilainen <ville.voutilainen@gmail.com>
Do the operator= SFINAE in the return type for optional,
......
......@@ -302,7 +302,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ __r.release(); }
template<typename _Tp, _Lock_policy _Lp>
template<typename _Tp1>
template<typename _Tp1, typename>
inline
__shared_ptr<_Tp, _Lp>::__shared_ptr(std::auto_ptr<_Tp1>&& __r)
: _M_ptr(__r.get()), _M_refcount()
......@@ -315,7 +315,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
}
template<typename _Tp>
template<typename _Tp1>
template<typename _Tp1, typename>
inline
shared_ptr<_Tp>::shared_ptr(std::auto_ptr<_Tp1>&& __r)
: __shared_ptr<_Tp>(std::move(__r)) { }
......
......@@ -34,7 +34,7 @@ test01()
{
std::shared_ptr<A> a;
std::auto_ptr<B> b;
a = std::move(b); // { dg-error "here" }
a = std::move(b); // { dg-error "no match" }
return 0;
}
......
// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++1z } }
// 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/>.
// 20.11.2.2.9 shared_ptr casts [util.smartptr.shared.cast]
#include <memory>
#include <testsuite_tr1.h>
struct MyP { virtual ~MyP() { }; };
struct MyDP : MyP { };
int main()
{
using __gnu_test::check_ret_type;
using std::shared_ptr;
using std::reinterpret_pointer_cast;
shared_ptr<double> spd;
shared_ptr<const int> spci;
shared_ptr<MyP> spa;
check_ret_type<shared_ptr<void> >(reinterpret_pointer_cast<void>(spd));
check_ret_type<shared_ptr<const short> >(reinterpret_pointer_cast<const short>(spci));
check_ret_type<shared_ptr<MyDP> >(reinterpret_pointer_cast<MyDP>(spa));
}
......@@ -25,10 +25,28 @@
struct A { };
int destroyed = 0;
struct B : A { ~B() { ++destroyed; } };
// 20.6.6.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from auto_ptr
int
template<typename From, typename To>
constexpr bool constructible()
{
using namespace std;
return is_constructible<shared_ptr<To>, auto_ptr<From>>::value
&& is_constructible<shared_ptr<const To>, auto_ptr<From>>::value
&& is_constructible<shared_ptr<const To>, auto_ptr<const From>>::value;
}
static_assert( constructible< A, A >(), "A -> A compatible" );
static_assert( constructible< B, A >(), "B -> A compatible" );
static_assert( constructible< int, int >(), "int -> int compatible" );
static_assert( !constructible< int, long >(), "int -> long not compatible" );
void
test01()
{
std::auto_ptr<A> a(new A);
......@@ -36,13 +54,24 @@ test01()
VERIFY( a.get() == 0 );
VERIFY( a2.get() != 0 );
VERIFY( a2.use_count() == 1 );
}
return 0;
void
test02()
{
std::auto_ptr<B> b(new B);
std::shared_ptr<A> a(std::move(b));
VERIFY( b.get() == 0 );
VERIFY( a.get() != 0 );
VERIFY( a.use_count() == 1 );
a.reset();
VERIFY( destroyed == 1 );
}
int
main()
{
test01();
test02();
return 0;
}
......@@ -24,10 +24,28 @@
struct A { };
int destroyed = 0;
struct B : A { ~B() { ++destroyed; } };
// 20.7.2.2.1 shared_ptr constructors [util.smartptr.shared.const]
// Construction from unique_ptr
int
template<typename From, typename To>
constexpr bool constructible()
{
using namespace std;
return is_constructible<shared_ptr<To>, unique_ptr<From>>::value
&& is_constructible<shared_ptr<const To>, unique_ptr<From>>::value
&& is_constructible<shared_ptr<const To>, unique_ptr<const From>>::value;
}
static_assert( constructible< A, A >(), "A -> A compatible" );
static_assert( constructible< B, A >(), "B -> A compatible" );
static_assert( constructible< int, int >(), "int -> int compatible" );
static_assert( !constructible< int, long >(), "int -> long not compatible" );
void
test01()
{
std::unique_ptr<A> up(new A);
......@@ -35,13 +53,24 @@ test01()
VERIFY( up.get() == 0 );
VERIFY( sp.get() != 0 );
VERIFY( sp.use_count() == 1 );
}
return 0;
void
test02()
{
std::unique_ptr<B> b(new B);
std::shared_ptr<A> a(std::move(b));
VERIFY( b.get() == 0 );
VERIFY( a.get() != 0 );
VERIFY( a.use_count() == 1 );
a.reset();
VERIFY( destroyed == 1 );
}
int
main()
{
test01();
test02();
return 0;
}
......@@ -26,3 +26,7 @@ void test01()
std::shared_ptr<void> p((void*)nullptr); // { dg-error "here" }
// { dg-error "incomplete" "" { target *-*-* } 0 }
}
using std::shared_ptr;
using std::is_constructible;
static_assert(!is_constructible<shared_ptr<void>, const void*>::value, "");
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