創(chuàng)建新的 std::shared_ptr 對象,它共享被管理對象的所有權(quán)箱蝠。若無被管理對象煤杀,即 *this 為空,則返回亦為空的 shared_ptr
谒出。
等效地返回 expired() ? shared_ptr<T>() : shared_ptr<T>(*this) 隅俘,原子地執(zhí)行邻奠。
參數(shù)
(無)
返回值
若 std::weak_ptr::expired 返回 false
則為共享被占有對象所有權(quán)的 shared_ptr
。否則返回默認(rèn)構(gòu)造的 T 類型的 shared_ptr
为居。
注意
此函數(shù)和 std::shared_ptr 的構(gòu)造函數(shù)可能獲得 std::weak_ptr
所指向的被管理對象的臨時所有權(quán)碌宴。區(qū)別是 std::shared_ptr 的構(gòu)造函數(shù)在其 std::weak_ptr
為空時拋異常,而 std::weak_ptr<T>::lock() 構(gòu)造空的 std::shared_ptr<T> 蒙畴。
示例代碼
#include <iostream>
#include <memory>
void observe(std::weak_ptr<int> weak)
{
if (auto observe = weak.lock()) {
std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
} else {
std::cout << "\tobserve() unable to lock weak_ptr<>\n";
}
}
int main()
{
std::weak_ptr<int> weak;
std::cout << "weak_ptr<> not yet initialized\n";
observe(weak);
{
auto shared = std::make_shared<int>(42);
weak = shared;
std::cout << "weak_ptr<> initialized with shared_ptr.\n";
observe(weak);
}
std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
observe(weak);
}
參考鏈接
https://zh.cppreference.com/w/cpp/memory/weak_ptr/lock
輸出
weak_ptr<> not yet initialized
observe() unable to lock weak_ptr<>
weak_ptr<> initialized with shared_ptr.
observe() able to lock weak_ptr<>, value=42
shared_ptr<> has been destructed due to scope exit.
observe() unable to lock weak_ptr<>
Program ended with exit code: 0
擴(kuò)展思考贰镣,變成class 之后
#include <iostream>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
class testTask{
public:
int taskId;
};
using testTaskPtr = std::shared_ptr<testTask>;
void observe(std::weak_ptr<int> weak)
{
if (auto observe = weak.lock()) {
std::cout << "\tobserve() able to lock weak_ptr<>, value=" << *observe << "\n";
} else {
std::cout << "\tobserve() unable to lock weak_ptr<>\n";
}
}
void observe_c(std::weak_ptr<testTask> weak)
{
if (auto observe = weak.lock()) {
std::cout << "\tobserve_c() able to lock weak_ptr<>, addr=" << observe << "\n";
} else {
std::cout << "\tobserve_c() unable to lock weak_ptr<>\n";
}
}
int main()
{
std::weak_ptr<int> weak;
std::cout << "weak_ptr<> not yet initialized\n";
observe(weak);
{
auto shared = std::make_shared<int>(42);
weak = shared;
std::cout << "weak_ptr<> initialized with shared_ptr.\n";
observe(weak);
}
std::cout << "shared_ptr<> has been destructed due to scope exit.\n";
observe(weak);
std::unordered_map<std::string,testTaskPtr> test_map;
auto test_share = std::make_shared<testTask>();
std::cout << "weak_ptr<> class initialized with shared_ptr.\n";
observe_c(test_share);
test_map["test"] = test_share;
test_map.erase("test");
std::cout << "weak_ptr<> class erase with shared_ptr.\n";
observe_c(test_share);
test_share = nullptr;
std::cout << "test_share = nullptr \n";
observe_c(test_share);
// test_map.insert (std::make_pair<std::string,testTask>("test_share",test_share));
}
輸出
weak_ptr<> not yet initialized
observe() unable to lock weak_ptr<>
weak_ptr<> initialized with shared_ptr.
observe() able to lock weak_ptr<>, value=42
shared_ptr<> has been destructed due to scope exit.
observe() unable to lock weak_ptr<>
weak_ptr<> class initialized with shared_ptr.
observe_c() able to lock weak_ptr<>, addr=0x10062b078
weak_ptr<> class erase with shared_ptr.
observe_c() able to lock weak_ptr<>, addr=0x10062b078
test_share = nullptr
observe_c() unable to lock weak_ptr<>
Program ended with exit code: 0