Gmane
From: Mark Santaniello (AMD <b-marks <at> microsoft.com>
Subject: Re: lambda and shared_ptr?
Newsgroups: gmane.comp.lib.boost.user
Date: 2005-11-22 19:47:38 GMT (2 years, 39 weeks, 6 days, 16 hours and 28 minutes ago)
> Mark Santaniello (AMD) wrote:
> >>> template< typename T >
> >>> boost::function<T(T)> foo( T n_ )
> >>> {
> >>>   //Works, but leaks n
> >>>   //T *n = new T( n_ );
> >>>
> >>>   //Doesn't work
> >>>   boost::shared_ptr<T> n( new T(n_) );
> >>>
> >>>   return ( *n += _1 );
> >>
> >> I think that you need to wrap n in boost::lambda::constant:
> >>
> >>     return *constant(n) += _1;
> >>
> >> Without it, *n is evaluated immediately.
> >>
> >>> }
> >>
> >
> > Peter,
> >
> > Thanks for the reply.  I must be doing something dumb, 
> because now I'm 
> > getting a build error.
> >
> > I think this *might* be the important part:
> > /usr/include/boost/detail/is_incrementable.hpp:59: error: 
> no match for 
> > 'operator++' in 
> > 
> '++boost::detail::is_incrementable_::impl<boost::shared_ptr<int> >::x'
> 
> Apparently, boost::detail::is_incrementable doesn't work on 
> your compiler; this in turn breaks boost::indirect_reference 
> and the return type deduction for lambda's operator*. But I 
> should also note that *constant(n) += _1 doesn't seem to work 
> even when the compiler supports is_incrementable, because for 
> some reason, Lambda thinks that dereferencing a const pointer 
> yields a const reference.
> 
>    return ret<int&>( *constant(pn) ) += _1;
> 
> compiles for me, though.
> 

Ahh, thanks Peter this helps a ton.  Seems like gcc 4.0.2 doesn't like
is_incrementable.  I tried gcc 3.3.6, however, and it worked fine.  I
also had success with MSVC 8.0.

-Mark