Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/enable shared from this/weak from this"

From cppreference.com
(+)
 
(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| num=1 | 1=
+
{{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| num=2 | 1=
+
{{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 {{c|std::shared_ptr}} that refer to {{c|*this}}.
+
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}}.
 
+
===Notes===
+
This is a copy of the the private mutable {{tt|weak_ptr}} member that is part of    {{tt|enabled_shared_from_this}}.  
+
  
 
===Return value===
 
===Return value===
{{c|std::weak_ptr<T>}} that shares ownership of {{c|*this}} with pre-existing {{c|std::shared_ptr}}s
+
{{box|{{rlpsi|/#weak_this}}}}
  
===Exceptions===
+
===Notes===
@2@ {{noexcept}}
+
{{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}}
  
[[de:cpp/memory/enable shared from this/weak from this]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/memory/enable shared from this/weak from this]]
+
[[fr:cpp/memory/enable shared from this/weak from this]]
+
[[it:cpp/memory/enable shared from this/weak from this]]
+
[[ja:cpp/memory/enable shared from this/weak from this]]
+
[[pt:cpp/memory/enable shared from this/weak from this]]
+
[[ru:cpp/memory/enable shared from this/weak from this]]
+
[[zh:cpp/memory/enable shared from this/weak from this]]
+

Latest revision as of 00:50, 22 October 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
 
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

weak_this

[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

[edit] See also

smart pointer with shared object ownership semantics
(class template) [edit]