Commit f3070dab by Jonathan Wakely Committed by Jonathan Wakely

Make std::enable_shared_from_this cope with ambiguity

	* include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&)): Call
	_M_enable_shared_from_this_with instead of
	__enable_shared_from_this_helper.
	* include/bits/shared_ptr.h (__enable_shared_from_this_helper):
	Remove overload for std::enable_shared_from_this..
	(__enable_shared_from_this_base): Define friend function to select a
	std::enable_shared_from_this base class.
	* include/bits/shared_ptr_base.h (__enable_shared_from_this_helper):
	Remove all overloads.
	(__shared_ptr): Change all relevant constructors to call
	_M_enable_shared_from_this_with instead of
	__enable_shared_from_this_helper.
	(__shared_ptr::__efst_base_t, __shared_ptr::__has_efst_base): Helpers
	to detect accessible and unambiguous enable_shared_from_this bases.
	(__shared_ptr::_M_enable_shared_from_this_with): New function to
	replace __enable_shared_from_this_helper overloads.
	(__enable_shared_from_this_helper): Remove overload for
	std::__enable_shared_from_this.
	(__enable_shared_from_this_base): Define friend function to select a
	std::__enable_shared_from_this base class.
	* include/experimental/bits/shared_ptr.h (experimental::shared_ptr):
	Change relevant constructors to call _M_enable_shared_from_this_with.
	(experimental::shared_ptr::__efst_base_t)
	(experimental::shared_ptr::__has_efst_base): Helpers to detect
	accessible and unambiguous enable_shared_from_this bases.
	(experimental::shared_ptr::_M_enable_shared_from_this_with): Define.
	(experimental::__enable_shared_from_this_helper): Remove overload for
	std::experimental::enable_shared_from_this.
	(experimental::__expt_enable_shared_from_this_base): Define friend
	function to select a std::experimental::enable_shared_from_this base.
	* testsuite/experimental/memory/shared_ptr/cons/
	enable_shared_from_this.cc: New test.
	* testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc:
	Adjust expected behaviour for shared_ptr<A[]>.

From-SVN: r241353
parent bdcf8e7c
2016-10-19 Jonathan Wakely <jwakely@redhat.com> 2016-10-19 Jonathan Wakely <jwakely@redhat.com>
* include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&)): Call
_M_enable_shared_from_this_with instead of
__enable_shared_from_this_helper.
* include/bits/shared_ptr.h (__enable_shared_from_this_helper):
Remove overload for std::enable_shared_from_this..
(__enable_shared_from_this_base): Define friend function to select a
std::enable_shared_from_this base class.
* include/bits/shared_ptr_base.h (__enable_shared_from_this_helper):
Remove all overloads.
(__shared_ptr): Change all relevant constructors to call
_M_enable_shared_from_this_with instead of
__enable_shared_from_this_helper.
(__shared_ptr::__efst_base_t, __shared_ptr::__has_efst_base): Helpers
to detect accessible and unambiguous enable_shared_from_this bases.
(__shared_ptr::_M_enable_shared_from_this_with): New function to
replace __enable_shared_from_this_helper overloads.
(__enable_shared_from_this_helper): Remove overload for
std::__enable_shared_from_this.
(__enable_shared_from_this_base): Define friend function to select a
std::__enable_shared_from_this base class.
* include/experimental/bits/shared_ptr.h (experimental::shared_ptr):
Change relevant constructors to call _M_enable_shared_from_this_with.
(experimental::shared_ptr::__efst_base_t)
(experimental::shared_ptr::__has_efst_base): Helpers to detect
accessible and unambiguous enable_shared_from_this bases.
(experimental::shared_ptr::_M_enable_shared_from_this_with): Define.
(experimental::__enable_shared_from_this_helper): Remove overload for
std::experimental::enable_shared_from_this.
(experimental::__expt_enable_shared_from_this_base): Define friend
function to select a std::experimental::enable_shared_from_this base.
* testsuite/experimental/memory/shared_ptr/cons/
enable_shared_from_this.cc: New test.
* testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc:
Adjust expected behaviour for shared_ptr<A[]>.
* include/debug/vector (__gnu_debug::vector::emplace_back): Fix return * include/debug/vector (__gnu_debug::vector::emplace_back): Fix return
type. type.
......
...@@ -311,7 +311,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -311,7 +311,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static_assert( sizeof(_Tp1) > 0, "incomplete type" ); static_assert( sizeof(_Tp1) > 0, "incomplete type" );
_Tp1* __tmp = __r.get(); _Tp1* __tmp = __r.get();
_M_refcount = __shared_count<_Lp>(std::move(__r)); _M_refcount = __shared_count<_Lp>(std::move(__r));
__enable_shared_from_this_helper(_M_refcount, __tmp, __tmp); _M_enable_shared_from_this_with(__tmp);
} }
template<typename _Tp> template<typename _Tp>
......
...@@ -607,25 +607,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -607,25 +607,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
{ _M_weak_this._M_assign(__p, __n); } { _M_weak_this._M_assign(__p, __n); }
template<typename _Tp1, typename _Tp2> // Found by ADL when this is an associated class.
friend void friend const enable_shared_from_this*
__enable_shared_from_this_helper(const __shared_count<>&, __enable_shared_from_this_base(const __shared_count<>&,
const enable_shared_from_this<_Tp1>*, const enable_shared_from_this* __p)
const _Tp2*) noexcept; { return __p; }
template<typename, _Lock_policy>
friend class __shared_ptr;
mutable weak_ptr<_Tp> _M_weak_this; mutable weak_ptr<_Tp> _M_weak_this;
}; };
template<typename _Tp1, typename _Tp2>
inline void
__enable_shared_from_this_helper(const __shared_count<>& __pn,
const enable_shared_from_this<_Tp1>*
__pe, const _Tp2* __px) noexcept
{
if (__pe != nullptr)
__pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn);
}
/** /**
* @brief Create an object that is owned by a shared_ptr. * @brief Create an object that is owned by a shared_ptr.
* @param __a An allocator. * @param __a An allocator.
......
...@@ -847,28 +847,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -847,28 +847,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_pi = nullptr; _M_pi = nullptr;
} }
// Support for enable_shared_from_this.
// Friend of __enable_shared_from_this.
template<_Lock_policy _Lp, typename _Tp1, typename _Tp2>
void
__enable_shared_from_this_helper(const __shared_count<_Lp>&,
const __enable_shared_from_this<_Tp1,
_Lp>*, const _Tp2*) noexcept;
// Friend of enable_shared_from_this.
template<typename _Tp1, typename _Tp2>
void
__enable_shared_from_this_helper(const __shared_count<>&,
const enable_shared_from_this<_Tp1>*,
const _Tp2*) noexcept;
template<_Lock_policy _Lp>
inline void
__enable_shared_from_this_helper(const __shared_count<_Lp>&, ...) noexcept
{ }
template<typename _Tp, _Lock_policy _Lp> template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr class __shared_ptr
{ {
...@@ -898,7 +876,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -898,7 +876,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
static_assert( !is_void<_Tp1>::value, "incomplete type" ); static_assert( !is_void<_Tp1>::value, "incomplete type" );
static_assert( sizeof(_Tp1) > 0, "incomplete type" ); static_assert( sizeof(_Tp1) > 0, "incomplete type" );
__enable_shared_from_this_helper(_M_refcount, __p, __p); _M_enable_shared_from_this_with(__p);
} }
template<typename _Tp1, typename _Deleter> template<typename _Tp1, typename _Deleter>
...@@ -907,7 +885,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -907,7 +885,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
// TODO requires _Deleter CopyConstructible and __d(__p) well-formed // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
__enable_shared_from_this_helper(_M_refcount, __p, __p); _M_enable_shared_from_this_with(__p);
} }
template<typename _Tp1, typename _Deleter, typename _Alloc> template<typename _Tp1, typename _Deleter, typename _Alloc>
...@@ -916,7 +894,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -916,7 +894,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ {
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
// TODO requires _Deleter CopyConstructible and __d(__p) well-formed // TODO requires _Deleter CopyConstructible and __d(__p) well-formed
__enable_shared_from_this_helper(_M_refcount, __p, __p); _M_enable_shared_from_this_with(__p);
} }
template<typename _Deleter> template<typename _Deleter>
...@@ -978,7 +956,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -978,7 +956,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>) __glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
auto __raw = _S_raw_ptr(__r.get()); auto __raw = _S_raw_ptr(__r.get());
_M_refcount = __shared_count<_Lp>(std::move(__r)); _M_refcount = __shared_count<_Lp>(std::move(__r));
__enable_shared_from_this_helper(_M_refcount, __raw, __raw); _M_enable_shared_from_this_with(__raw);
} }
#if _GLIBCXX_USE_DEPRECATED #if _GLIBCXX_USE_DEPRECATED
...@@ -1114,7 +1092,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1114,7 +1092,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// This relies on _Sp_counted_ptr_inplace::_M_get_deleter. // This relies on _Sp_counted_ptr_inplace::_M_get_deleter.
void* __p = _M_refcount._M_get_deleter(typeid(__tag)); void* __p = _M_refcount._M_get_deleter(typeid(__tag));
_M_ptr = static_cast<_Tp*>(__p); _M_ptr = static_cast<_Tp*>(__p);
__enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); _M_enable_shared_from_this_with(_M_ptr);
} }
#else #else
template<typename _Alloc> template<typename _Alloc>
...@@ -1146,7 +1124,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1146,7 +1124,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__shared_count<_Lp> __count(__ptr, __del, __del._M_alloc); __shared_count<_Lp> __count(__ptr, __del, __del._M_alloc);
_M_refcount._M_swap(__count); _M_refcount._M_swap(__count);
_M_ptr = __ptr; _M_ptr = __ptr;
__enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr); _M_enable_shared_from_this_with(_M_ptr);
} }
#endif #endif
...@@ -1166,6 +1144,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1166,6 +1144,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
friend class __weak_ptr<_Tp, _Lp>; friend class __weak_ptr<_Tp, _Lp>;
private: private:
template<typename _Yp>
using __esft_base_t = decltype(__enable_shared_from_this_base(
std::declval<const __shared_count<_Lp>&>(),
std::declval<_Yp*>()));
// Detect an accessible and unambiguous enable_shared_from_this base.
template<typename _Yp, typename = void>
struct __has_esft_base
: false_type { };
template<typename _Yp>
struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
: true_type { };
template<typename _Yp>
typename enable_if<__has_esft_base<_Yp>::value>::type
_M_enable_shared_from_this_with(const _Yp* __p) noexcept
{
if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
__base->_M_weak_assign(const_cast<_Yp*>(__p), _M_refcount);
}
template<typename _Yp>
typename enable_if<!__has_esft_base<_Yp>::value>::type
_M_enable_shared_from_this_with(const _Yp*) noexcept
{ }
void* void*
_M_get_deleter(const std::type_info& __ti) const noexcept _M_get_deleter(const std::type_info& __ti) const noexcept
{ return _M_refcount._M_get_deleter(__ti); } { return _M_refcount._M_get_deleter(__ti); }
...@@ -1579,26 +1585,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1579,26 +1585,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
{ _M_weak_this._M_assign(__p, __n); } { _M_weak_this._M_assign(__p, __n); }
template<_Lock_policy _Lp1, typename _Tp1, typename _Tp2> friend void
friend void __enable_shared_from_this_base(const __shared_count<_Lp>&,
__enable_shared_from_this_helper(const __shared_count<_Lp1>&, const __enable_shared_from_this* __p)
const __enable_shared_from_this<_Tp1, { return __p; }
_Lp1>*, const _Tp2*) noexcept;
mutable __weak_ptr<_Tp, _Lp> _M_weak_this; mutable __weak_ptr<_Tp, _Lp> _M_weak_this;
}; };
template<_Lock_policy _Lp1, typename _Tp1, typename _Tp2>
inline void
__enable_shared_from_this_helper(const __shared_count<_Lp1>& __pn,
const __enable_shared_from_this<_Tp1,
_Lp1>* __pe,
const _Tp2* __px) noexcept
{
if (__pe != nullptr)
__pe->_M_weak_assign(const_cast<_Tp2*>(__px), __pn);
}
template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args> template<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
inline __shared_ptr<_Tp, _Lp> inline __shared_ptr<_Tp, _Lp>
__allocate_shared(const _Alloc& __a, _Args&&... __args) __allocate_shared(const _Alloc& __a, _Args&&... __args)
......
...@@ -259,7 +259,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -259,7 +259,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// { // {
// void* __p = _M_refcount._M_get_deleter(typeid(__tag)); // void* __p = _M_refcount._M_get_deleter(typeid(__tag));
// _M_ptr = static_cast<_Tp*>(__p); // _M_ptr = static_cast<_Tp*>(__p);
// __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
// } // }
// __weak_ptr::lock() // __weak_ptr::lock()
...@@ -557,7 +556,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -557,7 +556,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// { // {
// void* __p = _M_refcount._M_get_deleter(typeid(__tag)); // void* __p = _M_refcount._M_get_deleter(typeid(__tag));
// _M_ptr = static_cast<_Tp*>(__p); // _M_ptr = static_cast<_Tp*>(__p);
// __enable_shared_from_this_helper(_M_refcount, _M_ptr, _M_ptr);
// } // }
// __weak_ptr::lock() // __weak_ptr::lock()
...@@ -740,16 +738,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -740,16 +738,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Tp1, typename = _SafeConv<_Tp1>> template<typename _Tp1, typename = _SafeConv<_Tp1>>
explicit explicit
shared_ptr(_Tp1* __p) : _Base_type(__p) { } shared_ptr(_Tp1* __p) : _Base_type(__p)
{ _M_enable_shared_from_this_with(__p); }
template<typename _Tp1, typename _Deleter, typename = _SafeConv<_Tp1>> template<typename _Tp1, typename _Deleter, typename = _SafeConv<_Tp1>>
shared_ptr(_Tp1* __p, _Deleter __d) shared_ptr(_Tp1* __p, _Deleter __d)
: _Base_type(__p, __d) { } : _Base_type(__p, __d)
{ _M_enable_shared_from_this_with(__p); }
template<typename _Tp1, typename _Deleter, typename _Alloc, template<typename _Tp1, typename _Deleter, typename _Alloc,
typename = _SafeConv<_Tp1>> typename = _SafeConv<_Tp1>>
shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a) shared_ptr(_Tp1* __p, _Deleter __d, _Alloc __a)
: _Base_type(__p, __d, __a) { } : _Base_type(__p, __d, __a)
{ _M_enable_shared_from_this_with(__p); }
template<typename _Deleter> template<typename _Deleter>
shared_ptr(nullptr_t __p, _Deleter __d) shared_ptr(nullptr_t __p, _Deleter __d)
...@@ -785,13 +786,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -785,13 +786,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#if _GLIBCXX_USE_DEPRECATED #if _GLIBCXX_USE_DEPRECATED
template<typename _Tp1, typename = _Compatible<_Tp1>> template<typename _Tp1, typename = _Compatible<_Tp1>>
shared_ptr(std::auto_ptr<_Tp1>&& __r) shared_ptr(std::auto_ptr<_Tp1>&& __r)
: _Base_type(std::move(__r)) { } : _Base_type(std::move(__r))
{ _M_enable_shared_from_this_with(static_cast<_Tp1*>(this->get())); }
#endif #endif
template<typename _Tp1, typename _Del, template<typename _Tp1, typename _Del,
typename = _UniqCompatible<_Tp1, _Del>> typename = _UniqCompatible<_Tp1, _Del>>
shared_ptr(unique_ptr<_Tp1, _Del>&& __r) shared_ptr(unique_ptr<_Tp1, _Del>&& __r)
: _Base_type(std::move(__r)) { } : _Base_type(std::move(__r))
{
// XXX assume conversion from __r.get() to this->get() to __elem_t*
// is a round trip, which might not be true in all cases.
using __elem_t = typename unique_ptr<_Tp1, _Del>::element_type;
_M_enable_shared_from_this_with(static_cast<__elem_t*>(this->get()));
}
constexpr shared_ptr(nullptr_t __p) constexpr shared_ptr(nullptr_t __p)
: _Base_type(__p) { } : _Base_type(__p) { }
...@@ -853,7 +861,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -853,7 +861,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a, shared_ptr(_Sp_make_shared_tag __tag, const _Alloc& __a,
_Args&&... __args) _Args&&... __args)
: _Base_type(__tag, __a, std::forward<_Args>(__args)...) : _Base_type(__tag, __a, std::forward<_Args>(__args)...)
{ } { _M_enable_shared_from_this_with(this->get()); }
template<typename _Tp1, typename _Alloc, typename... _Args> template<typename _Tp1, typename _Alloc, typename... _Args>
friend shared_ptr<_Tp1> friend shared_ptr<_Tp1>
...@@ -863,6 +871,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -863,6 +871,35 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: _Base_type(__r, std::nothrow) { } : _Base_type(__r, std::nothrow) { }
friend class weak_ptr<_Tp>; friend class weak_ptr<_Tp>;
template<typename _Yp>
using __esft_base_t =
decltype(__expt_enable_shared_from_this_base(std::declval<_Yp*>()));
// Detect an accessible and unambiguous enable_shared_from_this base.
template<typename _Yp, typename = void>
struct __has_esft_base
: false_type { };
template<typename _Yp>
struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
: __bool_constant<!is_array_v<_Tp>> { }; // ignore base for arrays
template<typename _Yp>
typename enable_if<__has_esft_base<_Yp>::value>::type
_M_enable_shared_from_this_with(const _Yp* __p) noexcept
{
if (auto __base = __expt_enable_shared_from_this_base(__p))
{
__base->_M_weak_this
= shared_ptr<_Yp>(*this, const_cast<_Yp*>(__p));
}
}
template<typename _Yp>
typename enable_if<!__has_esft_base<_Yp>::value>::type
_M_enable_shared_from_this_with(const _Yp*) noexcept
{ }
}; };
// C++14 §20.8.2.2.7 //DOING // C++14 §20.8.2.2.7 //DOING
...@@ -1258,15 +1295,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -1258,15 +1295,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
{ _M_weak_this._M_assign(__p, __n); } { _M_weak_this._M_assign(__p, __n); }
template<typename _Tp1> // Found by ADL when this is an associated class.
friend void friend const enable_shared_from_this*
__enable_shared_from_this_helper(const __shared_count<>& __pn, __expt_enable_shared_from_this_base(const enable_shared_from_this* __p)
const enable_shared_from_this* __pe, { return __p; }
const _Tp1* __px) noexcept
{ template<typename>
if(__pe != 0) friend class shared_ptr;
__pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
}
mutable weak_ptr<_Tp> _M_weak_this; mutable weak_ptr<_Tp> _M_weak_this;
}; };
......
// 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/>.
// { dg-do run { target c++14 } }
#include <experimental/memory>
#include <testsuite_hooks.h>
struct A : std::enable_shared_from_this<A> { };
struct B : std::experimental::enable_shared_from_this<B> { };
struct C : A, B { };
void
test01()
{
// This should not fail to compile due to ambiguous base classes:
std::experimental::shared_ptr<C> p(new C);
// And both base classes should have been enabled:
std::shared_ptr<A> pa = p->A::shared_from_this();
VERIFY( pa != nullptr );
// Can't compare pa and p because they're different types
std::experimental::shared_ptr<B> pb = p->B::shared_from_this();
VERIFY( pb != nullptr );
VERIFY( pb == p );
}
int
main()
{
test01();
}
...@@ -83,7 +83,16 @@ test02() ...@@ -83,7 +83,16 @@ test02()
VERIFY( sp.get() != 0 ); VERIFY( sp.get() != 0 );
VERIFY( sp.use_count() == 1 ); VERIFY( sp.use_count() == 1 );
VERIFY( sp[0].shared_from_this() != nullptr ); bool caught = false;
try
{
sp[0].shared_from_this(); // should not be set for arrays
}
catch (const std::bad_weak_ptr&)
{
caught = true;
}
VERIFY( caught );
sp.reset(); sp.reset();
VERIFY( destroyed == 5 ); VERIFY( destroyed == 5 );
......
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