Namespaces
Variants
Views
Actions

std::weak_ptr::lock

From cppreference.com
 
 
 
 
 
std::shared_ptr<T> lock() const
(since C++11)

Creates a new shared_ptr that shares ownership of the managed object. If there is no managed object, i.e. *this is empty, then the returned shared_ptr also is empty.

Effectively returns expired() ? shared_ptr<T>() : shared_ptr<T>(*this).

Contents

[edit] Parameters

(none)

[edit] Return value

A shared_ptr which shares ownership of the owned object.

[edit] Exceptions

noexcept specification:  
noexcept
  (since C++11)

[edit] Notes

Both this function and the constructor of shared_ptr may be used to acquire temporary ownership of the managed object. The difference is that the constructor of std::shared_ptr<T> throws an exception when the std::weak_ptr argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr<T>.

[edit] Example

[edit] See also

checks whether the referenced object was already deleted
(public member function) [edit]