
----- Original Message ----- From: "Peter Dimov" <pdimov@pdimov.com> To: <boost@lists.boost.org> Sent: Sunday, July 27, 2008 5:25 PM Subject: Re: [boost] [smart_ptr] make_shared usage
Daniel Frey:
Hi,
I have a question on make_shared. Consider a class which intends to force all instances to be handled via a shared_ptr. I used to do this by putting the ctors into the private section and adding static create()-methods in the public section. Example:
class X { private: X( int, double );
public: static shared_ptr<X> create( int i, double d ) { return shared_ptr<X>( new X( i, d ) ); } };
Replacing the returned expression with make_shared<X>( i, d ) wouldn't work, since it requires a public ctor.
You should be able to make it work by making the appropriate make_shared overload a friend:
template< class T, class A1, class A2 > friend boost::shared_ptr< T > boost::make_shared( A1 const & a1, A2 const & a2 );
If you declare the boost::make_shared friend, the utility of the create function is questionable. shared_ptr<X> x1_ptr = X::create(0,0); shared_ptr<X> x2_ptr = boost::make_shared <X>(0,0); The make_shared<X> function states clearly that the result is a shared_ptr<X>. Vicente