[LLVMdev] [cfe-dev] Use of Smart Pointers in LLVM Projects
David Blaikie
dblaikie at gmail.com
Fri Jul 18 10:17:55 PDT 2014
On Thu, Jul 17, 2014 at 8:20 PM, Zachary Turner <zturner at google.com> wrote:
>
>
>
> On Thu, Jul 17, 2014 at 4:58 PM, David Blaikie <dblaikie at gmail.com> wrote:
>>
>> On Thu, Jul 17, 2014 at 4:45 PM, Alp Toker <alp at nuanti.com> wrote:
>> > In many cases there shouldn't be a need for smart pointers at all,
>> > because
>> > the object is owned by a central facility's smart pointer so it's safe
>> > to
>> > just pass around the pointer in ephemeral contexts like stack-based
>> > instances. In such context raw pointers and references aren't scary --
>> > they're just fine.
>>
>> There's no contention there - if the pointer is non-owning, there's no
>> smart pointer to use. Indeed using a smart pointer for a non-owning
>> pointer would actually break the code by causing double deletes, etc.
>>
>> Well, shared_ptr notwithstanding - but, yes, choosing between
>> unique_ptr + non-owning pointers and shared_ptr can sometimes be
>> non-obvious, but I'm hopeful we generally agree that if there is a
>> dominating owner they can be given exclusive ownership through a
>> unique_ptr and everyone else can use raw pointers.
>
>
> What about weak_ptr?
If you need the auto-nulling behavior (usually because you've got a
non-owning, but longer-lifetime than the owner), sure. & maybe if
needed we can do something like AssertingVH (essentially a weak_ptr
that asserts on null access in debug builds and it's just a raw
pointer in release builds - a debugging aid for the cases where you
think you can use a raw pointer (shorter lifetime than the owning
pointer) but are investigating possible lifetime bugs) around
weak_ptr.
- David
More information about the llvm-dev
mailing list