std::enable_shared_from_this<T>::weak_from_this

From cppreference.com
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
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.

Return value

std::weak_ptr<T> that shares ownership of *this with pre-existing std::shared_ptrs.

Notes

weak_from_this copies the exposition only weak_ptr member that is part of enable_shared_from_this.

Feature-test macro Value Std Comment
__cpp_lib_enable_shared_from_this 201603L (C++17) std::enable_shared_from_this::weak_from_this

Example

See also

smart pointer with shared object ownership semantics
(class template)