Difference between revisions of "cpp/memory/enable shared from this/weak from this"
From cppreference.com
< cpp | memory | enable shared from this
(+) |
(Wording update.) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/memory/enable_shared_from_this/title | weak_from_this}} | + | {{cpp/memory/enable_shared_from_this/title|weak_from_this}} |
{{cpp/memory/enable_shared_from_this/navbar}} | {{cpp/memory/enable_shared_from_this/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl | since=c++17| | + | {{dcl|num=1|since=c++17| |
− | std::weak_ptr<T> weak_from_this(); | + | std::weak_ptr<T> weak_from_this() noexcept; |
}} | }} | ||
− | {{dcl | since=c++17| | + | {{dcl|num=2|since=c++17| |
− | std::weak_ptr<T const> weak_from_this() const; | + | std::weak_ptr<T const> weak_from_this() const noexcept; |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | Returns a {{c|std::weak_ptr<T>}} that tracks ownership of {{c|*this}} by all existing {{ | + | Returns a {{c/core|std::weak_ptr<T>}} that tracks ownership of {{c|*this}} by all existing {{lc|std::shared_ptr}} that refer to {{c|*this}}. |
− | + | ||
− | + | ||
− | + | ||
===Return value=== | ===Return value=== | ||
− | {{ | + | {{box|{{rlpsi|/#weak_this}}}} |
− | === | + | ===Notes=== |
− | + | {{feature test macro|__cpp_lib_enable_shared_from_this|std=C++17|value=201603L|{{tt|std::enable_shared_from_this::weak_from_this}}}} | |
===Example=== | ===Example=== | ||
Line 29: | Line 26: | ||
{{dsc end}} | {{dsc end}} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 00:50, 22 October 2024
std::weak_ptr<T> weak_from_this() noexcept; |
(1) | (since C++17) |
std::weak_ptr<T const> weak_from_this() const noexcept; |
(2) | (since C++17) |
Returns a std::weak_ptr<T> that tracks ownership of *this by all existing std::shared_ptr that refer to *this.
Contents |
[edit] Return value
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_enable_shared_from_this |
201603L | (C++17) | std::enable_shared_from_this::weak_from_this
|
[edit] Example
This section is incomplete Reason: no example |
[edit] See also
(C++11) |
smart pointer with shared object ownership semantics (class template) |