std::weak_ptr<T>::weak_ptr
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Memory management library
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
Garbage collector support (until C++23)
(exposition only*)
(C++11)
(C++23)
(C++11)
(C++17)
(C++11)
(C++11)
(C++20)
(C++20)
(C++17)
(C++11)
(C++17)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(until C++20*)
(until C++20*)
(until C++20*)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)
(C++11)
(C++17)
(C++20)
(C++17)
(C++11)
(C++11)
(C++11)
(until C++17*)
(C++11)
(C++17)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++11)
(C++20)
(C++11)
(C++11)
(C++20)
(C++26)
std::weak_ptr
Member functions
weak_ptr::weak_ptr
Modifiers
Observers
(C++26)
(C++26)
Non-member functions
Helper classes
(C++20)
Deduction guides (C++17)
constexpr weak_ptr() noexcept;
(1)
(since C++11)
weak_ptr( const weak_ptr& r ) noexcept;
(2)
(since C++11)
template< class Y >
weak_ptr( const weak_ptr<Y>& r ) noexcept;
(2)
(since C++11)
weak_ptr( const weak_ptr<Y>& r ) noexcept;
template< class Y >
weak_ptr( const std::shared_ptr <Y>& r ) noexcept;
(2)
(since C++11)
weak_ptr( const std::shared_ptr <Y>& r ) noexcept;
weak_ptr( weak_ptr&& r ) noexcept;
(3)
(since C++11)
template< class Y >
weak_ptr( weak_ptr<Y>&& r ) noexcept;
(3)
(since C++11)
weak_ptr( weak_ptr<Y>&& r ) noexcept;
Constructs new weak_ptr
that potentially shares an object with r.
1) Default constructor. Constructs empty
weak_ptr
.2) Constructs new
weak_ptr
which shares an object managed by r. If r manages no object, *this manages no object too. The templated overloads don't participate in the overload resolution unless Y*
is implicitly convertible to T*
, or Y
is the type "array of N
U
" for some type U
and some number N
, and T
is the type "array of unknown bound of (possibly cv-qualified) U
"(since C++17).3) Move constructors. Moves a weak_ptr instance from r into *this. After this, r is empty and r.use_count() == 0. The templated overload doesn't participate in the overload resolution unless
Y*
is implicitly convertible to T*
.[edit] Parameters
[edit] Notes
Because the default constructor is constexpr
, static std::weak_ptr s are initialized as part of static non-local initialization, before any dynamic non-local initialization begins. This makes it safe to use a std::weak_ptr in a constructor of any static object.
[edit] Example
Run this code
#include <iostream> #include <memory> 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'; }
Output:
w_ptr.use_count() inside scope: 1 w_ptr.use_count() out of scope: 0 w_ptr.expired() out of scope: true
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2315 | C++11 | move semantic was not enabled for weak_ptr
|
enabled |