make_shared and allocate_shared for classes with private constructors

I have some classes that derive from enable_shared_from_this which I've given private constructors and static creation methods which return a shared_ptr. This prevents someone from using the class inappropriately. It seems obvious that at a minimum using make_shared internally here would be an improvement. Unfortunately this requires that make_shared be made a friend for every arity and probably won't work with the variadic template implementation of make_shared. Is it possible to make declaring friendship for make_shared and allocate_shared simpler? I thinking something like serialization does with friend class boost::serialization::access. Thanks, -- Michael Marcin

Michael Marcin wrote:
I have some classes that derive from enable_shared_from_this which I've given private constructors and static creation methods which return a shared_ptr. This prevents someone from using the class inappropriately. It seems obvious that at a minimum using make_shared internally here would be an improvement. Unfortunately this requires that make_shared be made a friend for every arity and probably won't work with the variadic template implementation of make_shared.
Is it possible to make declaring friendship for make_shared and allocate_shared simpler? I thinking something like serialization does with friend class boost::serialization::access.
ping -- Michael Marcin

Michael Marcin:
I have some classes that derive from enable_shared_from_this which I've given private constructors and static creation methods which return a shared_ptr. This prevents someone from using the class inappropriately. It seems obvious that at a minimum using make_shared internally here would be an improvement. Unfortunately this requires that make_shared be > made a friend for every arity and probably won't work with the variadic template implementation of make_shared.
Is it possible to make declaring friendship for make_shared and allocate_shared simpler? I thinking something like serialization does with friend class boost::serialization::access.
One possible solution that springs to mind is to make allocate_shared use A::construct instead of doing placement new. You would then be able to make the allocator a friend. But please do submit a Trac ticket for this if you haven't already. -- Peter Dimov http://www.pdplayer.com

Peter Dimov wrote:
Michael Marcin:
I have some classes that derive from enable_shared_from_this which I've given private constructors and static creation methods which return a shared_ptr. This prevents someone from using the class inappropriately. It seems obvious that at a minimum using make_shared internally here would be an improvement. Unfortunately this requires that make_shared be > made a friend for every arity and probably won't work with the variadic template implementation of make_shared.
Is it possible to make declaring friendship for make_shared and allocate_shared simpler? I thinking something like serialization does with friend class boost::serialization::access.
One possible solution that springs to mind is to make allocate_shared use A::construct instead of doing placement new. You would then be able to make the allocator a friend.
But please do submit a Trac ticket for this if you haven't already.
Done: https://svn.boost.org/trac/boost/ticket/2481 -- Michael Marcin
participants (2)
-
Michael Marcin
-
Peter Dimov