My recent refactoring of enable_shared_from_this broke support for an
obscure use case, where shared_ptr "owns" a pointer to function (and
the deleter does something with it, like unregister a callback from a
plugin).
The problem is simply that I made the new
_M_enable_shared_from_this_with overloads take a const _Yp* parameter,
so that _Yp would be deduced as the non-const type. That was just
laziness, to save using remove_const on the type. But that breaks
function pointers, because const _Yp* won't deduce a type like
void(*)(), and so there is no viable overload of
_M_enable_shared_from_this_with.
The fix is just to use a parameter of _Yp* and then use remove_cv<_Yp>
to get the unqualified type.
I'm also adjusting a test which uses const_pointer_cast<X> to cast a
shared_ptr<const X> to shared_ptr<X>, but the object it owns was born
const so casting it away and accessing it as non-const is undefined.
PR libstdc++/80229
* include/bits/shared_ptr_base.h
(__shared_ptr::_M_enable_shared_from_this_with): Change parameters to
non-const and then use remove_cv to get unqualified type.
* testsuite/20_util/enable_shared_from_this/members/const.cc: Don't
cast away constness on object created const.
* testsuite/20_util/shared_ptr/cons/80229.cc: New test.
Tested powerpc64le-linux, committed to trunk.
commit fc31b73a950f957b7b5df2b56cb9285fac3edcd3
Author: Jonathan Wakely <[email protected]>
Date: Tue Mar 28 01:50:14 2017 +0100
PR libstdc++/80229 restore support for shared_ptr<function type>
PR libstdc++/80229
* include/bits/shared_ptr_base.h
(__shared_ptr::_M_enable_shared_from_this_with): Change parameters to
non-const and then use remove_cv to get unqualified type.
* testsuite/20_util/enable_shared_from_this/members/const.cc: Don't
cast away constness on object created const.
* testsuite/20_util/shared_ptr/cons/80229.cc: New test.
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h
b/libstdc++-v3/include/bits/shared_ptr_base.h
index a203f42..c8d7f20 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -1363,17 +1363,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
struct __has_esft_base<_Yp, __void_t<__esft_base_t<_Yp>>>
: __not_<is_array<_Tp>> { }; // No enable shared_from_this for arrays
- template<typename _Yp>
- typename enable_if<__has_esft_base<_Yp>::value>::type
- _M_enable_shared_from_this_with(const _Yp* __p) noexcept
+ template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
+ typename enable_if<__has_esft_base<_Yp2>::value>::type
+ _M_enable_shared_from_this_with(_Yp* __p) noexcept
{
if (auto __base = __enable_shared_from_this_base(_M_refcount, __p))
- __base->_M_weak_assign(const_cast<_Yp*>(__p), _M_refcount);
+ __base->_M_weak_assign(const_cast<_Yp2*>(__p), _M_refcount);
}
- template<typename _Yp>
- typename enable_if<!__has_esft_base<_Yp>::value>::type
- _M_enable_shared_from_this_with(const _Yp*) noexcept
+ template<typename _Yp, typename _Yp2 = typename remove_cv<_Yp>::type>
+ typename enable_if<!__has_esft_base<_Yp2>::value>::type
+ _M_enable_shared_from_this_with(_Yp*) noexcept
{ }
void*
diff --git
a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/const.cc
b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/const.cc
index 0f77726..3f8a6a5 100644
--- a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/const.cc
+++ b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/members/const.cc
@@ -32,9 +32,9 @@ test01()
{
struct X : public std::enable_shared_from_this<X> { };
using CX = const X;
- std::shared_ptr<CX> p(new X);
+ std::shared_ptr<CX> p(new CX);
VERIFY( share_ownership(p->shared_from_this(), p) );
- p.reset(new CX);
+ p.reset(new X);
VERIFY( share_ownership(p->shared_from_this(), p) );
auto p2 = std::const_pointer_cast<X>(p)->shared_from_this();
VERIFY( share_ownership(p2, p) );
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/cons/80229.cc
b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/80229.cc
new file mode 100644
index 0000000..7b89066
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/cons/80229.cc
@@ -0,0 +1,26 @@
+// Copyright (C) 2017 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 compile { target c++11 } }
+
+#include <memory>
+
+// PR libstdc++/80229
+
+void del(void(*)());
+void the_func();
+std::shared_ptr<void()> ee(&the_func, &del);