Boost logo

Boost Users :

Subject: Re: [Boost-users] [Bind] Understanding protect
From: Thomas Heller (thom.heller_at_[hidden])
Date: 2012-03-27 15:24:42


On 03/27/2012 10:17 PM, Robert Jones wrote:
> On Tue, Mar 27, 2012 at 7:25 PM, Gordon Woodhull <gordon_at_[hidden]
> <mailto:gordon_at_[hidden]>> wrote:
>
>
> On Mar 27, 2012, at 2:13 PM, Robert Jones wrote:
> > Ultimately I'm doing this for each member of a
> > range, via std algorithm, so I need to bind a unary argument to
> produce a nullary function
> > object, which I can then pass to an evaluation context, ie my g(
> ) function. If I've generated
> > the function object with bind and placeholder(s), it seems
> there's no way to prevent the binding
> > of the last placeholder from triggering evaluation, when what I
> require is an unevaluated nullary
> > function object.
>
> Hmm, works for me, IIUC:
>
> #include <boost/bind.hpp>
> #include <iostream>
>
> template<typename F>
> void g(F const &f) {
> f();
> }
>
> void f(int x) {
> std::cout << "x is " << x << std::endl;
> }
>
> int main() {
> for(int i=0; i<7; ++i)
> g(boost::bind(f,i));
> return 0;
> }
>
>
> But how would that translate to this context...?
>
> int main( )
> {
> std::vector<int> v;
> v += 0,1,2,3,4,5,6;
> for_each( v.begin( ), v.end( ), /* ???? */ );
> return 0;
> }
>
> Thx, Rob.

You might also want to consider phoenix::lambda, it is like protect, but
slightly more powerful.
your example would then read:
#include <boost/phoenix.hpp>
int main( )
{
      std::vector<int> v;
      v += 0,1,2,3,4,5,6;
      using boost::phoenix::bind;
      using boost::phoenix::lambda;
      using boost::phoenix::placeholders::_1;
      using boost::phoenix::local_names::_1;
      for_each( v.begin( ), v.end( ),
          bind(g, lambda(_a = _1)[bind(f, _a)]));
      return 0;
}


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