]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix ambiguity with multiple enable_shared_from_this bases
authorJonathan Wakely <jwakely@redhat.com>
Wed, 16 Dec 2015 17:09:47 +0000 (17:09 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 16 Dec 2015 17:09:47 +0000 (17:09 +0000)
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: r231701

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/shared_ptr.h
libstdc++-v3/include/bits/shared_ptr_base.h
libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc [new file with mode: 0644]

index ddae62f66b2d6df8cd60937f5f70e8495180c300..8a1a86af099e5ea40f9328844cd095c9e3f0c4ee 100644 (file)
@@ -1,3 +1,15 @@
+2015-12-16  Jonathan Wakely  <jwakely@redhat.com>
+
+       Backport from mainline
+       2015-12-02  Jonathan Wakely  <jwakely@redhat.com>
+
+       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-15  Tim Shen  <timshen@google.com>
 
        Backport from mainline
index 081d3bd3748d35e0b6e09c01b90878e341de025f..7ee224aae2e3bc2b389130d881d45550374b554e 100644 (file)
@@ -556,19 +556,25 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        _M_weak_assign(_Tp1* __p, const __shared_count<>& __n) const noexcept
        { _M_weak_this._M_assign(__p, __n); }
 
-      template<typename _Tp1>
+      template<typename _Tp1, typename _Tp2>
        friend void
-       __enable_shared_from_this_helper(const __shared_count<>& __pn,
-                                        const enable_shared_from_this* __pe,
-                                        const _Tp1* __px) noexcept
-       {
-         if (__pe != 0)
-           __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<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.
    *  @param  __a     An allocator.
index 026c5979faeb0cd5d88b586f6cf6be1f81b40f1d..64394f283ba00dc0da9a7bf362d1eb1285a68f43 100644 (file)
@@ -1506,19 +1506,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<typename _Tp1>
+      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 != 0)
-           __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<typename _Tp, _Lock_policy _Lp, typename _Alloc, typename... _Args>
     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 (file)
index 0000000..ea0f28d
--- /dev/null
@@ -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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++11" }
+
+#include <memory>
+#include <testsuite_hooks.h>
+
+struct A : std::enable_shared_from_this<A>
+{
+    void* a() { return shared_from_this().get(); }
+};
+
+struct B : std::enable_shared_from_this<B>
+{
+};
+
+struct D : A, B
+{
+};
+
+void test01()
+{
+  bool test = false;
+
+  auto d = std::make_shared<D>();
+  try
+  {
+      d->a();
+  }
+  catch (const std::bad_weak_ptr&)
+  {
+    test = true;
+  }
+  VERIFY(test);
+}
+
+int
+main()
+{
+  test01();
+}