Boost logo

Boost Users :

Subject: Re: [Boost-users] Lambda and placeholders
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2012-03-30 14:16:57


On 03/30/2012 07:19 PM, Sven Bauhan wrote:
> Hi,
>
> I just tried to use boost::lambda to prevent from creating an extra Functor
> class just for one call.
> This is the code I tried:
>
> std::deque<LightningSlice>::const_iterator l
> = std::find_if( m_lightnings.begin(), m_lightnings.end(),
> boost::lambda::_1.timeslice().contains(time_) );
>
> with
> typedef std::deque<LightningSlice> LightningQueue;
> LightningQueue m_lightnings; //!< The lightnings in timeslices
>
> const boost::posix_time::time_period& LightningSlice::timeslice() const
> {
> return this->m_period;
> }
>
> Then I get the error:
> ../LightningIndex.cpp:38: error: 'const struct
> boost::lambda::lambda_functor<boost::lambda::placeholder<1> >' has no member
> named 'timeslice'
>
> What did I wrong?

The type of the placeholder is completely different from the type the
placeholder will be eventually be replaced with. This in fact could be
any type in the world. What you need to do is bind it:

bind( // We want to bind the result of the innermost bind to another
       // function
     &boost::posix_time::time_period::contains
   , bind(
         // We want to bind the timeslice function to the first argument
         // passed
         &LightningQueue::timeslice
       , _1
     )
)

The flavor of bind you want to use is not important. There are currently
3 possibilities in boost (and a 4th if you use C++11):
http://www.boost.org/doc/libs/release/libs/bind/
http://www.boost.org/doc/libs/1_49_0/doc/html/lambda/le_in_details.html#lambda.bind_expressions
http://boost-sandbox.sourceforge.net/libs/phoenix/doc/html/phoenix/modules/bind.html

Boost.Bind is really just to bind functions. Lambda and Phoenix allow to
build more sophisticated unnamed functions. Phoenix is the newest
incarnation.
>
> Thanks,
> Sven
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net