Boost logo

Boost :

Subject: Re: [boost] Case study: Boost.Local versus Boost.Phoenix
From: Christian Holmquist (c.holmquist_at_[hidden])
Date: 2011-02-04 09:47:42


>
> 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
> }
>
>
>
It's not just you.
I've made a habit (which have been widely adapted by others at my workplace)
to make local lambdas like this:

void my_function()
{
   foo_type foo;
   bar_type bar;

  struct {
      foo_type &foo_;
      bar_type &bar_;
      void operator()(a_type a) const
      {
           foo_(a);
           bar_(a);
      }
   } lambda = { foo, bar} ;

   for_each(as.begin(),as.end(),lambda);

}

I don't know if that's standard compliant, but works perfectly fine in MSVC.
I like the {} initializer, so I don't need to write a boilerplate
constructor.

- Christian


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