
On Fri, Feb 4, 2011 at 10:55 AM, Mathias Gaunard <mathias.gaunard@ens-lyon.org> wrote:
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)
Sorry, C++ preprocessor only -- that was a requirement for me... (Can you imagine how much the syntax can be simplified with varidiac macros... I can!! Too bad I can't use them...). I still think Boost.Local syntax is better that your macro above but that's just because I am used to it, I know. However, I would like people to try the parenthesized syntax for a couple of days before saying it's horrible because in my experience it looks horrible at first but then it's not too bad... I guess I would just be much more useful for me to know if people have _used_ the parenthesized syntax and they _concluded_ it's horrible (instead of knowing the obvious that the parenthesis _look_ confusing at first). -- Lorenzo