|
Boost : |
Subject: Re: [boost] Case study: Boost.Local versus Boost.Phoenix
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2011-02-04 15:39:56
Artyom wrote:
> I'm sorry is it only me or it would be much more readable
> and maintainable to write:
>
> namespace {
> struct my_lambda {
> foo_type &foo;
> bar_type &bar
> my_lambda(foo_type &local_foo,bar_type &local_bar) :
> foo(local_foo),
> bar(local_bar)
> {
> }
>
> void operator()(a_type a) const
> {
> /// Your body goes there
> }
>
> };
> }
>
> void my_function()
> {
> foo_type foo;
> bar_type bar;
>
> my_lambda lambda(foo,bar);
> for_each(as.begin(),as.end(),lambda);
> // or something else
> }
No, it's not only you. I'd probably write something like that if a
simple bind(&fn,_1) were insufficient.
The main complaint about that from the advocates of both Pheonix and
Local seems to be that it's too far from the point of use. Well, I
have to ask how big your functions are, and if they're huge, why? Also
do you dislike code like this:
void f1(args) { ........ }
void my_big_fn() {
....
....
....
f1(a); // using a function only to avoid duplicating the
f1(b); // same code for a and b.
....
}
because the definition of f1 is far from where it's used? Would you
countenance using a macro instead:?
#define f1(arg) ....
f1(a);
f1(b);
Perhaps it comes down to this: we're used to functions being relatively
far from where they're used because it has always been like that, but
we think of these lambda expressions as taking the place of the body of
a loop, and we're not used to that being out-of-line.
Anyway, I'm just going to wait for C++0x lambdas.
Regards, Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk