Subject: Re: [boost] [smart_ptr] Interest in the missing smart pointer (that can target the stack)
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2016-01-28 21:07:38
On 29/01/2016 14:07, Nat Goodspeed wrote:
> Do you mean, for example, that you can create a registered_ptr to an
> instance within the constructor? (I haven't looked at your
FWIW, you can do that with enable_shared_from_raw, provided that you can
guarantee that someone will create a shared_ptr from the result of the
construction. (Otherwise you get a memory leak.)
> Heh. I've wished for some time for the ability to constrain a class:
> "this cannot be static" or "this cannot be instantiated on the stack"
> or "this cannot be instantiated on the heap." But I've never been
> sufficiently motivated to write a proposal -- or, come to that, search
> through WG21 archives for an existing proposal.
You can do "this cannot be instantiated on the stack" fairly easily --
make a private constructor with a static factory method that returns a
unique_ptr or shared_ptr.
In the latter case, this is also a good way to guarantee that a given
class has shared ownership, for use with enable_shared_from_this or as
an alternate method for the above to create pointers outside the "real"
constructor while still inside the "perceived" constructor.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk