std::weak_ptr::weak_ptr
constexpr weak_ptr(); | (1) | (since C++11) |
|---|---|---|
weak_ptr( const weak_ptr& r ); | (2) | (since C++11) |
template< class Y > weak_ptr( const weak_ptr<Y>& r ); | (2) | (since C++11) |
template< class Y > weak_ptr( const std::shared_ptr<Y>& r ); | (2) | (since C++11) |
weak_ptr( weak_ptr&& r ); | (3) | (since C++14) |
template< class Y > weak_ptr( weak_ptr<Y>&& r ); | (3) | (since C++14) |
构造新weak_ptr可能共享对象的r...
1%29默认构造函数。空构weak_ptr...
2%29新建weak_ptr共享由r.如果r不管理对象,*this也不管理对象。模板化重载不参与重载解析,除非Y*隐式可转换为T*,或Y的类型“数组。NU“对于某种类型U和一些数字N,和T类型“数组的未知界限为%28可能是cv-限定%29。U“.%28自C++17%29
3%29移动构造器。动弱[医]PTR实例r进*this.在这之后,r是空的r.use_count()==0模板化重载不参与重载解决方案,除非Y*隐式可转换为T*
参数
r | - | a std::shared_ptr or std::weak_ptr that will be viewed by this std::weak_ptr |
|---|
例外
noexcept规格:
noexcept
注记
因为默认构造函数是constexpr,静态弱[医]PTRS被初始化为静态非局部初始化,在任何动态的非本地初始化开始之前。这使得使用弱者是安全的。[医]任何静态对象的构造函数中的PTR。
例
二次
#include <memory>
#include <iostream>
struct Foo {};
int main()
{
std::weak_ptr<Foo> w_ptr;
{
auto ptr = std::make_shared<Foo>();
w_ptr = ptr;
std::cout << "w_ptr.use_count() inside scope: " << w_ptr.use_count() << '\n';
}
std::cout << "w_ptr.use_count() out of scope: " << w_ptr.use_count() << '\n';
std::cout << "w_ptr.expired() out of scope: " << std::boolalpha << w_ptr.expired() << '\n';
}二次
产出:
二次
w_ptr.use_count() inside scope: 1
w_ptr.use_count() out of scope: 0
w_ptr.expired() out of scope: true二次
另见
operator= | assigns the weak_ptr (public member function) |
|---|
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

