I'm reading the Boost
documentation for weak_ptr
here and in the beginning it says:
When the last shared_ptr to the object goes away and the object is deleted, the attempt to obtain a shared_ptr from the weak_ptr instances that refer to the deleted object will fail: the constructor will throw an exception of type boost::bad_weak_ptr, and weak_ptr::lock will return an empty shared_ptr.
Everything clear so far, however, 2 lines later it says:
weak_ptr operations never throw exceptions.
What does each statement refer to?
The weak_ptr
class never throws on any operation, internally, what happens when you call lock()
is that it catches the exception thrown by the constructor of the shared_ptr
(bad_weak_ptr
) and then returns you an empty shared_ptr
. Have a look at the source of the lock()
function for yourself to confirm.