Using lambda bind as destruct functor to shared_ptr

Hello, Should the following code sample work? It is the simplest reproduction case I could come up with: #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/shared_ptr.hpp> #include <stdlib.h> using namespace std; using namespace boost; using namespace boost::lambda; int main(void) { void * myResource = malloc(5); shared_ptr<void> mySharedResource(myResource, bind(free, _1)); } I get a compile error using g++ 4.0.1 on Mac OS X: /Documents/boost_1_34_0/boost/detail/sp_counted_impl.hpp: In member function 'void* boost::detail::sp_counted_impl_pd<P, D>::get_deleter(const std::type_info&) [with P = void*, D = boost::lambda::lambda_functor<boost::lambda:: lambda_functor_base<boost::lambda:: action<2, boost::lambda::function_action<2, void> >, boost::tuples::tuple<void (&)(void*), const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >]': test.cpp:13: instantiated from here /Documents/boost_1_34_0/boost/detail/sp_counted_impl.hpp:150: error: no match for ternary 'operator?:' in '(+ ti)->std::type_info::operator==(((const std::type_info&)((const std::type_info*)(& _ZTIN5boost6lambda14lambda_functorINS0_19lambda_functor_baseINS0_6action ILi2ENS0 _15function_actionILi2EvEEEENS_6tuples5tupleIRFvPvEKNS1_INS0_11placehold erILi1EE EEENS7_9null_typeESG_SG_SG_SG_SG_SG_SG_EEEEEE)))) ? boost::lambda::operator&(const boost::lambda::lambda_functor<T>&) [with Arg = boost::lambda::lambda_functor_base<boost::lambda::action<2, boost::lambda::function_action<2, void> >, boost::tuples::tuple<void (&)(void*), const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> >]() : 0' Of course, just using "free" instead of bind(free, _1) works, but my real case is more complicated and this appears to be a general conflict between lambda::bind and shared_ptr. Thanks, Scott French

Scott French wrote:
Hello,
Should the following code sample work? It is the simplest reproduction case I could come up with:
#include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/shared_ptr.hpp> #include <stdlib.h>
using namespace std; using namespace boost; using namespace boost::lambda;
int main(void) { void * myResource = malloc(5); shared_ptr<void> mySharedResource(myResource, bind(free, _1)); }
I get a compile error using g++ 4.0.1 on Mac OS X:
... My personal opinion is that overloading unary & is evil. My professional opinion is that the code should work. :-) Please apply the following patch (already in CVS): diff -u -r1.4 sp_counted_impl.hpp --- boost/detail/sp_counted_impl.hpp 11 Nov 2005 21:06:08 -0000 1.4 +++ boost/detail/sp_counted_impl.hpp 4 Jul 2007 16:34:16 -0000 @@ -147,7 +147,7 @@ virtual void * get_deleter( std::type_info const & ti ) { - return ti == typeid(D)? &del: 0; + return ti == typeid(D)? &reinterpret_cast<char&>( del ): 0; } #if defined(BOOST_SP_USE_STD_ALLOCATOR) @@ -217,7 +217,7 @@ virtual void * get_deleter( std::type_info const & ti ) { - return ti == typeid( D )? &d_: 0; + return ti == typeid( D )? &reinterpret_cast<char&>( d_ ): 0; } };
participants (2)
-
Peter Dimov
-
Scott French