
Dan Walters wrote:
I am posting to propose a new light weight pointer object library (boost.valid_ptr) that provides safe validity checking without having to take ownership or the the responsibility of memory management of the object pointed to.
...
Detailed proposal: https://docs.google.com/document/pub?id=1wod4CbeL9x8rLJ2S8815akMAGmYj-fCq90T...
This only works in single-threaded code, and even then, it can fail. In MT code, it's possible that the object is destroyed after you called get() and before you had the opportunity to look at the result (which is now a dangling pointer). In ST code, things get interesting when in: valid_ptr<X> px; px->f(); f() indirectly causes the object to be destroyed. This happens more often than one might think. :-) weak_ptr requires one to create a shared_ptr not because I wanted things to be less efficient, but because this shared_ptr keeps the object alive; this way, when you receive a positive answer from weak_ptr::lock, you can be sure it stays positive. Unless you're using null deleters of course, but the risk is all yours then. :-)