|
Boost : |
Subject: Re: [boost] [smart_ptr] Interest in the missing smart pointer (that can target the stack)
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2016-01-28 21:09:15
On Thu, Jan 28, 2016 at 5:53 PM, Gavin Lambert <gavinl_at_[hidden]>
wrote:
> On 29/01/2016 14:35, Emil Dotchevski wrote:
>
>> On Thu, Jan 28, 2016 at 12:20 PM, Noah <duneroadrunner_at_[hidden]> 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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk