std::bad_weak_ptr - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::bad_weak_ptr

From cppreference.com
< cpp‎ | memory

Defined in header `<memory>` |  |
class bad_weak_ptr; |  | (since C++11)

`std::bad_weak_ptr` is the type of the object thrown as exceptions by the constructors of std::shared_ptr that take std::weak_ptr as the argument, when the std::weak_ptr refers to an already deleted object.

Inheritance diagram

### [edit] Member functions

(constructor) | constructs the `bad_weak_ptr` object (public member function)

## Inherited from std::exception

### Member functions

(destructor) [virtual] | destructs the exception object (virtual public member function of `std::exception`) [edit]
what [virtual] | returns an explanatory string (virtual public member function of `std::exception`) [edit]

### [edit] Example

Run this code

    #include <memory>
    #include <iostream>
    int main()
    {
        std::shared_ptr<int> p1(new int(42));
        std::weak_ptr<int> wp(p1);
        p1.reset();
        try {
            std::shared_ptr<int> p2(wp);
        } catch(const std::bad_weak_ptr& e) {
            std::cout << e.what() << '\n';
        }
    }

Output:

    std::bad_weak_ptr

### [edit] See also

shared_ptr (C++11) | smart pointer with shared object ownership semantics (class template) [edit]
weak_ptr (C++11) | weak reference to an object managed by std::shared_ptr (class template) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/bad_weak_ptr&oldid=62770"