---
name: "std::weak_ptr"
section: "3"
platform: "linux"
package: "gcc"
version: "16.2.1+r23+gd564253eb6c8-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/std::weak_ptr"
---

# std::weak\_ptr(3)

A non-owning observer for a pointer owned by a shared\_ptr.

## NAME

std::weak\_ptr\< \_Tp \> - A non-owning observer for a pointer owned by a shared\_ptr.

## SYNOPSIS

\#include \<memory\>

### Public Types

using **element\_type**\

### Public Member Functions

template\<typename \_Yp, typename = \_Constructible\<const shared\_ptr\<\_Yp\>&\>\> **weak\_ptr** (const **shared\_ptr**\< \_Yp \> &\_\_r) noexcept\
 **weak\_ptr** (const weak\_ptr &) noexcept=default\
 template\<typename \_Yp, typename = \_Constructible\<const weak\_ptr\<\_Yp\>&\>\> **weak\_ptr** (const weak\_ptr\< \_Yp \> &\_\_r) noexcept\
 **weak\_ptr** (weak\_ptr &&) noexcept=default\
 template\<typename \_Yp, typename = \_Constructible\<weak\_ptr\<\_Yp\>\>\> **weak\_ptr** (weak\_ptr\< \_Yp \> &&\_\_r) noexcept\
 bool **expired** () const noexcept\
 **shared\_ptr**\< \_Tp \> **lock** () const noexcept\
 template\<typename \_Yp\> \_Assignable\< const **shared\_ptr**\< \_Yp \> & \> **operator=** (const **shared\_ptr**\< \_Yp \> &\_\_r) noexcept\
 weak\_ptr & **operator=** (const weak\_ptr &\_\_r) noexcept=default\
 template\<typename \_Yp\> \_Assignable\< const weak\_ptr\< \_Yp \> & \> **operator=** (const weak\_ptr\< \_Yp \> &\_\_r) noexcept\
 weak\_ptr & **operator=** (weak\_ptr &&\_\_r) noexcept=default\
 template\<typename \_Yp\> \_Assignable\< weak\_ptr\< \_Yp \> \> **operator=** (weak\_ptr\< \_Yp \> &&\_\_r) noexcept\
 template\<typename \_Tp1\> bool **owner\_before** (const \_\_shared\_ptr\< \_Tp1, \_Lp \> &\_\_rhs) const noexcept\
 template\<typename \_Tp1\> bool **owner\_before** (const \_\_weak\_ptr\< \_Tp1, \_Lp \> &\_\_rhs) const noexcept\
 void **reset** () noexcept\
 void **swap** (\_\_weak\_ptr &\_\_s) noexcept\
 long **use\_count** () const noexcept\

### Related Symbols

(Note that these are not member symbols.)  template\<typename \_Tp\> void **swap** (weak\_ptr\< \_Tp \> &\_\_a, weak\_ptr\< \_Tp \> &\_\_b) noexcept\
 Swap overload for weak\_ptr.

## Detailed Description

### template\<typename \_Tp\>

class std::weak\_ptr\< \_Tp \>"A non-owning observer for a pointer owned by a shared\_ptr.

**Since**

C++11

A weak\_ptr provides a safe alternative to a raw pointer when you want a non-owning reference to an object that is managed by a shared\_ptr.

Unlike a raw pointer, a weak\_ptr can be converted to a new shared\_ptr that shares ownership with every other shared\_ptr that already owns the pointer. In other words you can upgrade from a non-owning "weak" reference to an owning shared\_ptr, without having access to any of the existing shared\_ptr objects.

Also unlike a raw pointer, a weak\_ptr does not become "dangling" after the object it points to has been destroyed. Instead, a weak\_ptr becomes *expired* and can no longer be converted to a shared\_ptr that owns the freed pointer, so you cannot accidentally access the pointed-to object after it has been destroyed.

## Author

Generated automatically by Doxygen for libstdc++ from the source code.
