Namespaces
Variants
Views
Actions

Talk:cpp/memory/shared ptr/shared ptr

From cppreference.com

The page says:

"When constructing a shared_ptr from a raw pointer to an object of a type derived from std::enable_shared_from_this, the constructors of shared_ptr detect other owners of the object through the weak reference stored in that base class. This allows ownership of the object to be shared with existing shared_ptrs. Otherwise, it is undefined behavior to construct a shared_ptr for an object that is already managed by another shared_ptr."

But this does not work in GCC. Is it in the C++ standard? Which paragraph?

--78.12.76.238 11:14, 3 February 2014 (PST)

This description follows §20.8.2.4[util.smartptr.enab]/11, which says "The shared_ptr constructors that create unique pointers can detect the presence of an enable_shared_from_this base and assign the newly created shared_ptr to its __weak_this member.". In LLVM libc++ this functionality is implemented by the call to __enable_weak_this from the shared_ptr's constructors. GNU libstdc++ makes a call to __enable_shared_from_this_helper from __shared_ptr's constructors. (note: this may have been worded poorly on this page, I'll review) --Cubbi (talk)
Yes, the description on this page was wrong, fixed. Thank you for bringing it up! --Cubbi (talk) 12:01, 3 February 2014 (PST)
Much better now. I made a small edit that makes wording even more explicit. I think that even a tiny misunderstanding of the role of std::enable_shared_from_this can bring to daunting bugs. Feel free to rollback my edit, though. --78.12.77.107 09:38, 4 February 2014 (PST)