std::weak_ptr<T>::weak_ptr

From cppreference.com
< cpp‎ | memory‎ | weak ptr
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Dynamic memory management
Uninitialized memory algorithms
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++20)

Constrained uninitialized memory algorithms
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Allocators
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
Garbage collection support
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)
(哋它亢++11)(until 哋它亢++23)



Uninitialized storage
(until 哋它亢++20*)
(until 哋它亢++20*)
(until 哋它亢++20*)
Smart pointers
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(until 哋它亢++17*)
(哋它亢++11)
(哋它亢++17)
(哋它亢++26)
(哋它亢++26)
(哋它亢++11)
(哋它亢++11)
(哋它亢++23)
(哋它亢++23)
Low level memory
management
(哋它亢++17)
Miscellaneous
(哋它亢++11)
(哋它亢++20)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)
C Library
(哋它亢++17)

 
std::weak_ptr
Member functions
weak_ptr::weak_ptr
Modifiers
Observers
(哋它亢++26)
(哋它亢++26)
Non-member functions
Helper classes
(哋它亢++20)
Deduction guides(哋它亢++17)
 
constexpr weak_ptr() noexcept;
(1) (since 哋它亢++11)
weak_ptr( const weak_ptr& r ) noexcept;
(2) (since 哋它亢++11)
template< class Y >
weak_ptr( const weak_ptr<Y>& r ) noexcept;
(2) (since 哋它亢++11)
template< class Y >
weak_ptr( const std::shared_ptr<Y>& r ) noexcept;
(2) (since 哋它亢++11)
weak_ptr( weak_ptr&& r ) noexcept;
(3) (since 哋它亢++11)
template< class Y >
weak_ptr( weak_ptr<Y>&& r ) noexcept;
(3) (since 哋它亢++11)

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 哋它亢++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*.

Parameters

r - a std::shared_ptr or std::weak_ptr that will be viewed by this std::weak_ptr

Notes

Because the default constructor is constexpr, static std::weak_ptrs 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.

Example

#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

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2315 哋它亢++11 move semantic was not enabled for weak_ptr enabled

See also

assigns the weak_ptr
(public member function)