[Boost-users] make_shared and custom deleter

391 views
Skip to first unread message

Dominique Devienne

unread,
May 27, 2009, 6:47:21 PM5/27/09
to boost-users
I was thinking of using the new make_shared, but I'm already using a
custom deleter (shared instances have private destructora and declare
the custom deleter as a friend, and new instances are returned wrapped
in shared_ptr, thus no explicit new or delete should be possible).

Looking at make_shared, I can't see a way to use both at the same
time. Am I out of luck? Thanks, --DD
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Frank Mori Hess

unread,
May 28, 2009, 10:48:22 AM5/28/09
to boost...@lists.boost.org
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 27 May 2009, Dominique Devienne wrote:
> I was thinking of using the new make_shared, but I'm already using a
> custom deleter (shared instances have private destructora and declare
> the custom deleter as a friend, and new instances are returned wrapped
> in shared_ptr, thus no explicit new or delete should be possible).
>
> Looking at make_shared, I can't see a way to use both at the same
> time. Am I out of luck? Thanks, --DD

You can't, not even with boost::allocate_shared since it doesn't use the
allocator for in-place construction. See ticket #2481:

https://svn.boost.org/trac/boost/ticket/2481

In theory you should be able to use std::allocate_shared in c++0x mode if your
compiler provides it. Define your own allocator (a bit of a pain) and make
it a friend of your class. Then add a static factory function to your class
with calls allocate_shared with your friend allocator.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkoepDYACgkQ5vihyNWuA4VxnQCdH8IKhh6XnLqZbx+H1fZ4mBXh
+voAn12BSApivL5uv3kIZes9vLAJZYpv
=5dK9
-----END PGP SIGNATURE-----

Dominique Devienne

unread,
May 28, 2009, 5:00:58 PM5/28/09
to boost...@lists.boost.org
On Thu, May 28, 2009 at 9:48 AM, Frank Mori Hess <frank...@nist.gov> wrote:
> On Wednesday 27 May 2009, Dominique Devienne wrote:
>> I was thinking of using the new make_shared, but I'm already using a
>> custom deleter (shared instances have private destructors and declare

>> the custom deleter as a friend, and new instances are returned wrapped
>> in shared_ptr, thus no explicit new or delete should be possible).
>>
>> Looking at make_shared, I can't see a way to use both at the same
>> time. Am I out of luck? Thanks, --DD
>
> You can't, not even with boost::allocate_shared since it doesn't use the
> allocator for in-place construction.  See ticket #2481:
>
> https://svn.boost.org/trac/boost/ticket/2481
>
> In theory you should be able to use std::allocate_shared in c++0x mode if your
> compiler provides it.  Define your own allocator (a bit of a pain) and make
> it a friend of your class.  Then add a static factory function to your class
> with calls allocate_shared with your friend allocator.

I'm not quite following everything but sounds like I can't have it
both ways for now. And I can't use C++0x, so I'll forget about
make_shared for the moment. Thanks for answering though. --DD

Reply all
Reply to author
Forward
0 new messages