
On 04/02/2011 13:35, 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 }
That's exactly the kind of thing Boost.Local does, it generates that structure but avoids you the boilerplate of declaring foo and bar members and forwarding them in the constructor. Also, it uses Boost.Typeof, so you don't have to write their type. Also, it is much more valuable to declare this as void my_function() { foo_type foo; bar_type bar; struct { 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 } } lambda(foo, bar); for_each(as.begin(),as.end(),lambda); // or something else } since the lambda is right next to where it is used (which is the whole point of the exercise). It is true however that the syntax of the Boost.Local macro could greatly be simplified. I think LOCAL_FUNCTION(R, f, (a, b, c)(T0 a0, T1 a1), body) is a better syntax. (a, b, c) would catch a, b, and c in the scope and would be optional. (Of course, it requires the use of the C99 preprocessor)