[cfe-dev] Use of Smart Pointers in LLVM Projects

David Blaikie dblaikie at gmail.com
Sun Aug 10 22:34:26 PDT 2014

On Jul 17, 2014 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,
>> > the object is owned by a central facility's smart pointer so it's safe
>> > 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?

I'd generally reserve them for use when the auto-null behavior is
necessary, for example in lazy caches. Though it seems the situations that
call for them are relatively rare - its a good tool when you need it.

I suppose in some cases it might be nice to have something like AssertingVH
that's a raw pointer in release builds and an asserting weak_ptr-like thing
in debug builds.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140810/94a069aa/attachment.html>

More information about the cfe-dev mailing list