Difference between revisions of "cpp/memory/enable shared from this"
m (Add "make_shared" See Also) |
m (fmt) |
||
Line 1: | Line 1: | ||
{{cpp/title|enable_shared_from_this}} | {{cpp/title|enable_shared_from_this}} | ||
{{cpp/memory/enable_shared_from_this/navbar}} | {{cpp/memory/enable_shared_from_this/navbar}} | ||
− | {{ddcl | header=memory | since=c++11 | 1= | + | {{ddcl|header=memory|since=c++11|1= |
template< class T > class enable_shared_from_this; | template< class T > class enable_shared_from_this; | ||
}} | }} | ||
Line 11: | Line 11: | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc prot mem ctor | cpp/memory/enable_shared_from_this/enable_shared_from_this | constructs an {{tt|enable_shared_from_this}} object }} | + | {{dsc prot mem ctor|cpp/memory/enable_shared_from_this/enable_shared_from_this|constructs an {{tt|enable_shared_from_this}} object }} |
− | {{dsc prot mem dtor | cpp/memory/enable_shared_from_this/~enable_shared_from_this | destroys an {{tt|enable_shared_from_this}} object }} | + | {{dsc prot mem dtor|cpp/memory/enable_shared_from_this/~enable_shared_from_this|destroys an {{tt|enable_shared_from_this}} object }} |
− | {{dsc prot mem fun | + | {{dsc prot mem fun|cpp/memory/enable_shared_from_this/operator{{=}}|returns a reference to {{c|this}}}} |
− | {{dsc mem fun | + | {{dsc mem fun|cpp/memory/enable_shared_from_this/shared_from_this|returns a {{c|shared_ptr}} which shares ownership of {{c|*this}}}} |
− | {{dsc mem fun | + | {{dsc mem fun|cpp/memory/enable_shared_from_this/weak_from_this|returns the {{c|weak_ptr}} which shares ownership of {{c|*this}}|notes={{mark c++17}}}} |
{{dsc end}} | {{dsc end}} | ||
===Member objects=== | ===Member objects=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem | Member name | Definition}} | + | {{dsc hitem|Member name|Definition}} |
− | {{dsc | {{tt|weak_this}} {{mark|private}}{{mark c++17}} | {{lc|std::weak_ptr}} object tracking the control block of the first shared owner of {{c|*this}}. Exposition only }} | + | {{dsc|{{tt|weak_this}} {{mark|private}}{{mark c++17}}|{{lc|std::weak_ptr}} object tracking the control block of the first shared owner of {{c|*this}}. Exposition only }} |
{{dsc end}} | {{dsc end}} | ||
Line 31: | Line 31: | ||
{{tt|enable_shared_from_this}} provides the safe alternative to an expression like {{c|std::shared_ptr<T>(this)}}, which is likely to result in {{c|this}} being destructed more than once by multiple owners that are unaware of each other (see example below). | {{tt|enable_shared_from_this}} provides the safe alternative to an expression like {{c|std::shared_ptr<T>(this)}}, which is likely to result in {{c|this}} being destructed more than once by multiple owners that are unaware of each other (see example below). | ||
− | {{feature test macro|__cpp_lib_enable_shared_from_this|std=C++17|value=201603L|More precise specification of {{tt|std::enable_shared_from_this}}}} | + | {{feature test macro|__cpp_lib_enable_shared_from_this|std=C++17|value=201603L|More precise specification of [[#Top|{{tt|std::enable_shared_from_this}}]]}} |
===Example=== | ===Example=== | ||
{{example | {{example | ||
− | + | | | |
− | + | |code= | |
#include <memory> | #include <memory> | ||
#include <iostream> | #include <iostream> | ||
Line 84: | Line 84: | ||
void misuseGood() | void misuseGood() | ||
{ | { | ||
− | // Bad: shared_from_this is called without having std::shared_ptr owning the caller | + | // Bad: shared_from_this is called without having std::shared_ptr owning the caller |
try { | try { | ||
Good not_so_good; | Good not_so_good; | ||
Line 90: | Line 90: | ||
} catch(std::bad_weak_ptr& e) { | } catch(std::bad_weak_ptr& e) { | ||
// undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) | // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) | ||
− | std::cout << e.what() << '\n'; | + | std::cout << e.what() << '\n'; |
} | } | ||
} | } | ||
Line 124: | Line 124: | ||
testBad(); | testBad(); | ||
} | } | ||
− | + | |p=true|output= | |
good1.use_count() = 2 | good1.use_count() = 2 | ||
bad_weak_ptr | bad_weak_ptr | ||
Line 131: | Line 131: | ||
Bad::~Bad() called | Bad::~Bad() called | ||
Bad::~Bad() called | Bad::~Bad() called | ||
− | *** glibc detected *** ./test: double free or corruption | + | *** glibc detected *** ./test: double free or corruption |
}} | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/memory/dsc shared_ptr}} | + | {{dsc inc|cpp/memory/dsc shared_ptr}} |
− | {{dsc inc | cpp/memory/shared_ptr/dsc make_shared}} | + | {{dsc inc|cpp/memory/shared_ptr/dsc make_shared}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Revision as of 07:21, 2 January 2023
Defined in header <memory>
|
||
template< class T > class enable_shared_from_this; |
(since C++11) | |
std::enable_shared_from_this
allows an object t
that is currently managed by a std::shared_ptr named pt
to safely generate additional std::shared_ptr instances pt1, pt2, ...
that all share ownership of t
with pt
.
Publicly inheriting from std::enable_shared_from_this<T>
provides the type T
with a member function shared_from_this
. If an object t
of type T
is managed by a std::shared_ptr<T> named pt
, then calling T::shared_from_this
will return a new std::shared_ptr<T> that shares ownership of t
with pt
.
Contents |
Member functions
constructs an enable_shared_from_this object (protected member function) | |
destroys an enable_shared_from_this object (protected member function) | |
returns a reference to this (protected member function) | |
returns a shared_ptr which shares ownership of *this (public member function) | |
(C++17) |
returns the weak_ptr which shares ownership of *this (public member function) |
Member objects
Member name | Definition |
weak_this (private)(C++17)
|
std::weak_ptr object tracking the control block of the first shared owner of *this. Exposition only |
Notes
A common implementation for enable_shared_from_this
is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr detect the presence of an unambiguous and accessible (ie. public inheritance is mandatory)(since C++17) enable_shared_from_this
base and assign the newly created std::shared_ptr to the internally stored weak reference if not already owned by a live std::shared_ptr(since C++17). Constructing a std::shared_ptr for an object that is already managed by another std::shared_ptr will not consult the internally stored weak reference and thus will lead to undefined behavior.
It is permitted to call shared_from_this
only on a previously shared object, i.e. on an object managed by std::shared_ptr<T>. Otherwise the behavior is undefined(until C++17)std::bad_weak_ptr is thrown (by the shared_ptr constructor from a default-constructed weak_this
)(since C++17).
enable_shared_from_this
provides the safe alternative to an expression like std::shared_ptr<T>(this), which is likely to result in this being destructed more than once by multiple owners that are unaware of each other (see example below).
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_enable_shared_from_this |
201603L | (C++17) | More precise specification of std::enable_shared_from_this
|
Example
#include <memory> #include <iostream> class Good : public std::enable_shared_from_this<Good> { public: std::shared_ptr<Good> getptr() { return shared_from_this(); } }; class Best : public std::enable_shared_from_this<Best> { public: std::shared_ptr<Best> getptr() { return shared_from_this(); } // No public constructor, only a factory function, // so there's no way to have getptr return nullptr. [[nodiscard]] static std::shared_ptr<Best> create() { // Not using std::make_shared<Best> because the c'tor is private. return std::shared_ptr<Best>(new Best()); } private: Best() = default; }; struct Bad { std::shared_ptr<Bad> getptr() { return std::shared_ptr<Bad>(this); } ~Bad() { std::cout << "Bad::~Bad() called\n"; } }; void testGood() { // Good: the two shared_ptr's share the same object std::shared_ptr<Good> good0 = std::make_shared<Good>(); std::shared_ptr<Good> good1 = good0->getptr(); std::cout << "good1.use_count() = " << good1.use_count() << '\n'; } void misuseGood() { // Bad: shared_from_this is called without having std::shared_ptr owning the caller try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } } void testBest() { // Best: Same but can't stack-allocate it: std::shared_ptr<Best> best0 = Best::create(); std::shared_ptr<Best> best1 = best0->getptr(); std::cout << "best1.use_count() = " << best1.use_count() << '\n'; // Best stackBest; // <- Will not compile because Best::Best() is private. } void testBad() { // Bad, each shared_ptr thinks it's the only owner of the object std::shared_ptr<Bad> bad0 = std::make_shared<Bad>(); std::shared_ptr<Bad> bad1 = bad0->getptr(); std::cout << "bad1.use_count() = " << bad1.use_count() << '\n'; } // UB: double-delete of Bad int main() { testGood(); misuseGood(); testBest(); testBad(); }
Possible output:
good1.use_count() = 2 bad_weak_ptr best1.use_count() = 2 bad1.use_count() = 1 Bad::~Bad() called Bad::~Bad() called *** glibc detected *** ./test: double free or corruption
See also
(C++11) |
smart pointer with shared object ownership semantics (class template) |
creates a shared pointer that manages a new object (function template) |