From 28eca950b760f1e98f0626a441ffd48b8baad153 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Wed, 2 Dec 2015 15:08:18 +0000 Subject: [PATCH] Fix ambiguity with multiple enable_shared_from_this bases PR libstdc++/56383 * testsuite/20_util/enable_shared_from_this/56383.cc: New. * include/bits/shared_ptr_base.h (__enable_shared_from_this): Make friend declaration match previous declaration of __enable_shared_from_this_helper. * include/bits/shared_ptr.h (enable_shared_from_this): Likewise. From-SVN: r231181 --- libstdc++-v3/ChangeLog | 9 +++ libstdc++-v3/include/bits/shared_ptr.h | 22 +++++--- libstdc++-v3/include/bits/shared_ptr_base.h | 22 +++++--- .../20_util/enable_shared_from_this/56383.cc | 56 +++++++++++++++++++ 4 files changed, 93 insertions(+), 16 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 337a4e2a3e3..eafe38c7ca1 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2015-12-02 Jonathan Wakely + + PR libstdc++/56383 + * testsuite/20_util/enable_shared_from_this/56383.cc: New. + * include/bits/shared_ptr_base.h (__enable_shared_from_this): Make + friend declaration match previous declaration of + __enable_shared_from_this_helper. + * include/bits/shared_ptr.h (enable_shared_from_this): Likewise. + 2015-12-01 Jonathan Wakely * include/experimental/bits/fs_path.h (path::_Cmpt): Move definition diff --git a/libstdc++-v3/include/bits/shared_ptr.h b/libstdc++-v3/include/bits/shared_ptr.h index 2413b1b8e10..26a0ad3d7b7 100644 --- a/libstdc++-v3/include/bits/shared_ptr.h +++ b/libstdc++-v3/include/bits/shared_ptr.h @@ -582,19 +582,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept { _M_weak_this._M_assign(__p, __n); } - template + template friend void - __enable_shared_from_this_helper(const __shared_count<>& __pn, - const enable_shared_from_this* __pe, - const _Tp1* __px) noexcept - { - if (__pe != nullptr) - __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); - } + __enable_shared_from_this_helper(const __shared_count<>&, + const enable_shared_from_this<_Tp1>*, + const _Tp2*) noexcept; mutable weak_ptr<_Tp> _M_weak_this; }; + template + 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. * @param __a An allocator. diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index 1a96b4cd36d..f4f98e6fdc4 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -1546,19 +1546,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept { _M_weak_this._M_assign(__p, __n); } - template + template<_Lock_policy _Lp1, typename _Tp1, typename _Tp2> friend void - __enable_shared_from_this_helper(const __shared_count<_Lp>& __pn, - const __enable_shared_from_this* __pe, - const _Tp1* __px) noexcept - { - if (__pe != nullptr) - __pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn); - } + __enable_shared_from_this_helper(const __shared_count<_Lp1>&, + const __enable_shared_from_this<_Tp1, + _Lp1>*, const _Tp2*) noexcept; 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 inline __shared_ptr<_Tp, _Lp> diff --git a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc new file mode 100644 index 00000000000..ea0f28da8f5 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc @@ -0,0 +1,56 @@ +// Copyright (C) 2015 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 +// . + +// { dg-options "-std=gnu++11" } + +#include +#include + +struct A : std::enable_shared_from_this +{ + void* a() { return shared_from_this().get(); } +}; + +struct B : std::enable_shared_from_this +{ +}; + +struct D : A, B +{ +}; + +void test01() +{ + bool test = false; + + auto d = std::make_shared(); + try + { + d->a(); + } + catch (const std::bad_weak_ptr&) + { + test = true; + } + VERIFY(test); +} + +int +main() +{ + test01(); +} -- 2.30.2