[shared_ptr] Using a custom delete function that is a class member function

I have a class that provides a shared_ptr number. At the end of the number's life cycle it should be placed back on a list of available numbers. The shared_ptr's custom deleter function looks like what I need. I'm able to get a free function to compile as the deleter just fine. In trying to use a class member function as the custom deleter I've run into compile errors. I'm not sure where I'm going wrong. I know the custom deleter need to be copy constructible and I'm probably breaking some assumed requirement. Any help on what I'm doing wrong would be appreciated. Ryan class CTNGenerator { public: CTNGenerator(void) { m_AvailableCTNs.resize(20); unsigned short startValue = 15; for (ctns::iterator itor = m_AvailableCTNs.begin(); itor != m_AvailableCTNs.end(); ++itor) { //This compiles fine. boost::bind(boost::mem_fun(&CTNGenerator::makeCTNAvailable), this); //Placing the call as the custom delete function doesn't compile. *itor = bus(new unsigned short(startValue++), boost::bind(boost::mem_fun(&CTNGenerator::makeCTNAvailable, this))); } } private: void CTNGenerator::makeCTNAvailable(unsigned short * ctn) { m_AvailableCTNs.push_back(boost::shared_ptr<unsigned short>(ctn)); } private: typedef boost::shared_ptr<unsigned short> bus; typedef std::list<bus> ctns; ctns m_AvailableCTNs; };

AMDG Ryan McConnehey wrote:
I have a class that provides a shared_ptr number. At the end of the number's life cycle it should be placed back on a list of available numbers. The shared_ptr's custom deleter function looks like what I need. I'm able to get a free function to compile as the deleter just fine. In trying to use a class member function as the custom deleter I've run into compile errors. I'm not sure where I'm going wrong. I know the custom deleter need to be copy constructible and I'm probably breaking some assumed requirement. Any help on what I'm doing wrong would be appreciated. Ryan
class CTNGenerator { public: CTNGenerator(void) { m_AvailableCTNs.resize(20); unsigned short startValue = 15;
for (ctns::iterator itor = m_AvailableCTNs.begin(); itor != m_AvailableCTNs.end(); ++itor) { //This compiles fine. boost::bind(boost::mem_fun(&CTNGenerator::makeCTNAvailable), this); //Placing the call as the custom delete function doesn't compile. *itor = bus(new unsigned short(startValue++), boost::bind(boost::mem_fun(&CTNGenerator::makeCTNAvailable, this))); } }
Check you parentheses. The two uses of bind/mem_fun are not the same. In Christ, Steven Watanabe

Ryan McConnehey wrote:
Steven Watanabe wrote:
Check you parentheses. The two uses of bind/mem_fun are not the same. Sorry, the line should read as follows.
*itor = bus(new unsigned short(startValue++), boost::bind(boost::mem_fun(&CTNGenerator::makeCTNAvailable), this));
You need boost::bind( &CTNGenerator::makeCTNAvailable, this, _1 ). bind will automatically insert the mem_fn for you. Being a member function, makeCTNAvailable takes two arguments: the hidden 'this' and 'ctn'. _1 routes the pointer argument of the deleter into its proper position as 'ctn'.

Peter Dimov wrote:
You need boost::bind( &CTNGenerator::makeCTNAvailable, this, _1 ). bind will automatically insert the mem_fn for you. Being a member function, makeCTNAvailable takes two arguments: the hidden 'this' and 'ctn'. _1 routes the pointer argument of the deleter into its proper position as 'ctn'. Thanks everyone for your suggestions. I was able to get the class behaving correctly by using Peter's solution.
Ryan

2009/10/10 Ryan McConnehey
I have a class that provides a shared_ptr number. At the end of the number's life cycle it should be placed back on a list of available numbers. The shared_ptr's custom deleter function looks like what I need. I'm able to get a free function to compile as the deleter just fine. In trying to use a class member function as the custom deleter I've run into compile errors. I'm not sure where I'm going wrong. I know the custom deleter need to be copy constructible and I'm probably breaking some assumed requirement. Any help on what I'm doing wrong would be appreciated. Ryan
I'm pretty sure you can't provide a functor as the custom deleter, only a free function. Probably due to space efficiency within the shared_ptr blob (4 byte pointer vs ~32 bytes for a boost::function). Alex

Since the deleter is parameterized, why space optimization be a concern.
I've never tried using a functor as a deleter, but I don't see why it
wouldn't work. If it was only a free function that would work, I don't
think they would need the parameterized type D.
Have you tried removing the boost::mem_fun part? I think the shared_ptr
constructor might not be able to deduce the return type of the member
function correctly. I'm basing my comment on this documentation:
http://www.boost.org/doc/libs/1_40_0/libs/bind/bind.html#with_member_pointer...
On Sat, Oct 10, 2009 at 3:13 AM, Alex MDC
2009/10/10 Ryan McConnehey
I have a class that provides a shared_ptr number. At the end of the number's life cycle it should be placed back on a list of available numbers. The shared_ptr's custom deleter function looks like what I need. I'm able to get a free function to compile as the deleter just fine. In trying to use a class member function as the custom deleter I've run into compile errors. I'm not sure where I'm going wrong. I know the custom deleter need to be copy constructible and I'm probably breaking some assumed requirement. Any help on what I'm doing wrong would be appreciated. Ryan
I'm pretty sure you can't provide a functor as the custom deleter, only a free function. Probably due to space efficiency within the shared_ptr blob (4 byte pointer vs ~32 bytes for a boost::function).
Alex
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (5)
-
Alex MDC
-
Peter Dimov
-
Ryan McConnehey
-
Shane Baker
-
Steven Watanabe