Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/bad weak ptr"

From cppreference.com
< cpp‎ | memory
(Member functions: add postcondition, with dr marker)
m (format)
Line 14: Line 14:
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc mem ctor | bad_weak_ptr() | nolink=true | constructs the bad_weak_ptr object}}
+
{{dsc mem ctor | cpp/memory/bad_weak_ptr | inlinemem=true | constructs the bad_weak_ptr object}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 58: Line 58:
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/memory/bad weak ptr]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/memory/bad weak ptr]]
+
[[fr:cpp/memory/bad weak ptr]]
+
[[it:cpp/memory/bad weak ptr]]
+
[[ja:cpp/memory/bad weak ptr]]
+
[[pt:cpp/memory/bad weak ptr]]
+
[[ru:cpp/memory/bad weak ptr]]
+
[[zh:cpp/memory/bad weak ptr]]
+

Revision as of 21:10, 26 August 2017

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(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)



 
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.

cpp/error/exceptionstd-bad weak ptr-inheritance.svg

Inheritance diagram

Contents

Member functions

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

std::bad_weak_ptr ::bad_weak_ptr()

bad_weak_ptr() noexcept;

Constructs a new instance of std::bad_weak_ptr. what() returns an implementation-defined null-terminated byte string.

Parameters

(none)

Inherited from std::exception

Member functions

[virtual]
destroys the exception object
(virtual public member function of std::exception) [edit]
[virtual]
returns an explanatory string
(virtual public member function of std::exception) [edit]

Example

#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

See also

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