std::weak_ptr<T>::lock (3) - Linux Man Pages
std::weak_ptr<T>::lock - std::weak_ptr<T>::lock
std::shared_ptr<T> lock() const noexcept; (since C++11)
Creates a new std::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), executed atomically.
A shared_ptr which shares ownership of the owned object if std::weak_ptr::expired returns false. Else returns default-constructed shared_ptr of type T.
Both this function and the constructor of std::shared_ptr may be used to acquire temporary ownership of the managed object referred to by a std::weak_ptr. The difference is that the constructor of std::shared_ptr throws an exception when its std::weak_ptr argument is empty, while std::weak_ptr<T>::lock() constructs an empty std::shared_ptr<T>.
// Run this code
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior
LWG_2316 C++11 lock() was not required to be atomic, but required to be noexcept, which led to a contradiction specified to be atomic
expired (public member function)