创建新的 std::shared_ptr 对象,它共享被管理对象的所有权。若无被管理对象,即 *this 为空,则返回亦为空的 shared_ptr
。
等效地返回 expired() ? shared_ptr<T>() : shared_ptr<T>(*this) ,原子地执行。
参数
(无)
返回值
若 std::weak_ptr::expired 返回 false
则为共享被占有对象所有权的 shared_ptr
。否则返回默认构造的 T 类型的 shared_ptr
。
注意
此函数和 std::shared_ptr 的构造函数可能获得 std::weak_ptr
所指向的被管理对象的临时所有权。区别是 std::shared_ptr 的构造函数在其 std::weak_ptr
为空时抛异常,而 std::weak_ptr<T>::lock() 构造空的 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
扩展思考,变成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
网友评论