On Thu, Jan 28, 2016 at 5:53 PM, Gavin Lambert
On 29/01/2016 14:35, Emil Dotchevski wrote:
On Thu, Jan 28, 2016 at 12:20 PM, Noah
wrote: registered_ptr can point to stack allocated objects (just like native C++
pointers can),
shared_ptr can point to stack-allocated objects, too. As a benefit, if you use shared_ptr to point at a stack-allocated object, you also get the weak_ptr functionality.
Yes, you can make a shared_ptr that points at a stack object (by using a null deleter) but AFAIK this doesn't actually work as expected unless you can guarantee that no shared_ptr instances will survive destruction of the stack frame (ie. it is only used within the call chain and never copied outside of it).
And it's way too easy to break that guarantee because it's not the semantics that shared_ptr was designed for.
{ foo local; shared_ptr<foo> pl(&local,null_deleter()); .... do_something(p); .... assert(pl.unique()); } Yes, in the presence of exceptions one must also assert(pl.unique()) in a catch(...), and yes, compile-time errors are better than run-time errors, but I wouldn't sacrifice the availability of weak_ptr and the capacity of shared_ptr to act as THE single smart pointer framework in a program. Emil