Boost logo

Boost :

From: Darren Cook (darren_at_[hidden])
Date: 2004-02-16 18:54:25


Thanks for the detailed reply Brian.

>>1. (from the paper)
>> int a[]={5,3,8,4};
>> std::for_each(a,a+4,std::cout<<_1<<"\n");
>
> In FC++ it'd look like this:
>
> std::for_each(a,a+4,
> lambda(X)[ &cout %out_stream% X %out_stream% '\n' ] );

The use of X is just tradition is it? I could just as easily use _1, or some
meaningful variable? Being able to name the variable is an advantage.

On the other hand %out_stream% is not as nice as <<. Is FC++ overloading
operator<< an option? I think the "Hello World" examples have to be as
attractive as possible to bring the punters in.

> std::for_each(a,a+4,
> lambda(X)[ &cout %out_stream% string("v=")
> %out_stream% X %out_stream% '\n' ] );
>
> Same thing here, with the added annoyance that you have to manually turn
> the string literal into some "by-value" type....

But after reading your source I realized this is doing a different job to
constant() in BLL isn't it. It is just the "..." string that is the problem,
not that the lambda variable is not the first thing in the expression.

>>3. (a vector of MyClass pointers)
>> std::for_each(
>> tests.begin(),
>> tests.end(),
>> bind(&MyClass::save,_1,f)
>> );
>
> Here you would probably do
>
> std::for_each( tests.begin(), tests.end(),
> ptr_to_fun(&MyClass::save)(_,5) );

(the 'f' was actually a FILE* but treating it as an int doesn't change
anything).

I was hoping you might have some way to get rid of the "&MyClass::" bit, so
I could write:
     lambda(test)[ test->save(f) ]

In most of my real code "MyClass" is actually a 60 character monster with
template parameters; I usually give in and a write a for() loop for that
reason (i.e. it is quicker to type the for() loop out than go hunting
through my code to get the exact class name definition).

> Note that if "save" were defined by MyClass as a functoid, things get
> even easier in the client code; see the attached example (and YourClass)
> for details.

Have you thought of supplying some macros to help - my eyes kept bouncing
off the code in YourClass.

>> bind(delete_ptr(), _1)

> std::for_each( tests.begin(), tests.end(), delete_ );

That is nice. I wonder why BLL didn't do this instead of delete_ptr()?

> ... the gist of it would again be that
>
> bind(&MyClass::on_name, this, _1,_2)
>
> would be spelled
>
> ptr_to_fun(&MyClass::on_name)(this)

Interesting. I cannot decide if I like _1,_2 disappearing or not (I'm trying
to think about
when I look at it in 6 months time).

Darren


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk