[llvm-bugs] [Bug 25380] New: enable_shared_from_this doesn't work when setting a pointer to a base class without enable_shared_from_this

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 2 11:30:23 PST 2015


https://llvm.org/bugs/show_bug.cgi?id=25380

            Bug ID: 25380
           Summary: enable_shared_from_this doesn't work when setting a
                    pointer to a base class without
                    enable_shared_from_this
           Product: libc++
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: agbakken at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

Created attachment 15204
  --> https://llvm.org/bugs/attachment.cgi?id=15204&action=edit
source file demonstrating the problem

The following example doesn't compile on clang --version: 
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin15.0.0
Thread model: posix

To build:

clang++ main.cpp -o enable_shared_from_this_bug
In file included from main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4032:35:
error: no viable overloaded '='
                __e->__weak_this_ = *this;
                ~~~~~~~~~~~~~~~~~ ^ ~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4070:5:
note: in instantiation of function template specialization
      'std::__1::shared_ptr<Base>::__enable_weak_this<Sub>' requested here
    __enable_weak_this(__p);
    ^
main.cpp:14:27: note: in instantiation of function template specialization
'std::__1::shared_ptr<Base>::shared_ptr<Sub>' requested here
    std::shared_ptr<Base> base(new Sub);
                          ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4984:15:
note: candidate function not viable: no known conversion from
      'std::__1::shared_ptr<Base>' to 'const std::__1::weak_ptr<Sub>' for 1st
argument
    weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4991:9:
note: candidate template ignored: could not match 'weak_ptr' against
'shared_ptr'
        operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:5009:13:
note: candidate template ignored: disabled by 'enable_if' [with _Yp = Base]
            is_convertible<_Yp*, element_type*>::value,
            ^
1 error generated.
make: *** [all] Error 1
This compile on gcc 5.2.1 on Ubuntu 15.10.

This patch fixes the problem:

--- /tmp/memory 2015-11-02 11:13:37.000000000 -0800
+++
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory
   2015-11-02 11:24:02.000000000 -0800
@@ -4029,7 +4029,7 @@
         __enable_weak_this(const enable_shared_from_this<_Yp>* __e) _NOEXCEPT
         {
             if (__e)
-                __e->__weak_this_ = *this;
+                __e->__weak_this_ = shared_ptr<_Yp>(*this,
static_cast<_Yp*>(get()));
         }

     _LIBCPP_INLINE_VISIBILITY

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20151102/4d67061b/attachment.html>


More information about the llvm-bugs mailing list