定义于头文件
template< class T > class weak_ptr; (C++11 起)
std::weak_ptr 是一种智能指针,它对被 std::shared_ptr 管理的对象存在非拥有性(「弱」)引用。在访问所引用的对象前必须先转换为 std::shared_ptr。
std::weak_ptr 用来表达临时所有权的概念:当某个对象只有存在时才需要被访问,而且随时可能被他人删除时,可以使用 std::weak_ptr 来跟踪该对象。需要获得临时所有权时,则将其转换为 std::shared_ptr,此时如果原来的 std::shared_ptr 被销毁,则该对象的生命期将被延长至这个临时的 std::shared_ptr 同样被销毁为止。
std::weak_ptr 的另一用法是打断 std::shared_ptr 所管理的对象组成的环状引用。若这种环被孤立(例如无指向环中的外部共享指针),则 shared_ptr 引用计数无法抵达零,而内存被泄露。能令环中的指针之一为弱指针以避免此情况。
std::swap(std::weak_ptr)
- template< class T >
- void swap( weak_ptr<T>& lhs, weak_ptr<T>& rhs ) noexcept; (C++11 起)
为 std::weak_ptr 特化 std::swap 算法。交换 lhs 与 rhs 的指针。调用 lhs.swap(rhs) 。
| lhs, rhs | - | 要交换内容的智能指针 |
(无)
常数
- #include <memory>
- #include <iostream>
-
- struct Foo
- {
- int N;
- Foo(int n): N(n) {}
- };
-
- int main()
- {
- std::shared_ptr<Foo> ptr_1 = std::make_shared<Foo>(1);
- std::shared_ptr<Foo> ptr_2 = std::make_shared<Foo>(2);
-
- std::weak_ptr<Foo> w_ptr1 = ptr_1;
- std::weak_ptr<Foo> w_ptr2 = ptr_2;
-
- {
- std::shared_ptr<Foo> ptr1 = w_ptr1.lock() ;
- std::shared_ptr<Foo> ptr2 = w_ptr2.lock() ;
- std::cout << "ptr1 N: " << ptr1->N << '\n';
- std::cout << "ptr2 N: " << ptr2->N << '\n';
- }
-
- {
- std::swap(w_ptr1, w_ptr2);
- std::shared_ptr<Foo> ptr1 = w_ptr1.lock() ;
- std::shared_ptr<Foo> ptr2 = w_ptr2.lock() ;
- // 只交换std::weak_ptr
- std::cout << "ptr1 N: " << ptr1->N << '\n';
- std::cout << "ptr2 N: " << ptr2->N << '\n';
- // 不交换std::shared_ptr
- std::cout << "ptr_1 N: " << ptr_1->N << '\n';
- std::cout << "ptr_1 N: " << ptr_2->N << '\n';
- }
- }

std::weak_ptr<T>::use_count
long use_count() const noexcept; (C++11 起)
返回共享被管理对象所有权的 shared_ptr 实例数量,或 0 ,若被管理对象已被删除,即 *this 为空。
(无)
在调用的瞬间共享被管理对象所有权的 shared_ptr 实例数量。
expired() 可能快于 use_count() 。此函数固有地有不稳,若被管理对象在可能创建并销毁 shared_ptr 副本的线程间共享:则结果仅若匹配调用方线程所独占的副本数或零才可靠;任何其他值可能在能使用前就变得过时了。
- #include <memory>
- #include <iostream>
-
- int main()
- {
- std::shared_ptr<int> s_ptr = std::make_shared<int>(3);
- std::weak_ptr<int> w_ptr = s_ptr;
-
- 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';
-
- std::shared_ptr<int> s_ptr1 = s_ptr;
- std::cout << "w_ptr1.use_count() out of scope: " << w_ptr.use_count() << '\n';
- std::cout << "w_ptr1.expired() out of scope: " << std::boolalpha << w_ptr.expired() << '\n';
-
- s_ptr1.reset();
- std::cout << "w_ptr1.use_count() out of scope: " << w_ptr.use_count() << '\n';
- std::cout << "w_ptr1.expired() out of scope: " << std::boolalpha << w_ptr.expired() << '\n';
- }

std::weak_ptr<T>::expired
bool expired() const noexcept; (C++11 起)
等价于 use_count() == 0 。可能仍未对被管理对象调用析构函数,但此对象的析构已经临近(或可能已发生)。
(无)
若被管理对象已被删除则为 true ,否则为 false 。
若被管理对象在线程间共享,则此函数内在地不可靠,通常 false 结果可能在能用之前就变得过时。 true 结果可靠。
- #include <iostream>
- #include <memory>
-
- std::weak_ptr<int> gw;
-
- void f()
- {
- if (!gw.expired())
- {
- std::cout << "gw is valid\n";
- }
- else
- {
- std::cout << "gw is expired\n";
- }
- }
-
- int main()
- {
- {
- auto sp = std::make_shared<int>(42);
- gw = sp;
-
- f();
- }
-
- f();
- }
