Boost logo

Boost :

From: Powell, Gary (powellg_at_[hidden])
Date: 2004-03-03 17:35:32


>> // PROPOSED CHANGE IN SOME FUTURE PAPER
>>
>> unsigned long n = std::accumulate(
>> xs.begin(), xs.end(), 0UL,
>> long (long lhs, X const &rhs) // inline fn with no name.
>> {
>> return lhs + rhs.count;
>> }
>> );

>Yeah, but do you propose to make local names visible inside the anonymous
>function, or not? If you do, you open a big can of worms for implementations
>(recursive nested functions, need I say more?), if you don't, BLL can still
>do things that anonymous functions can not. Anonymous functors could help
>some, but their syntax won't be quite as compact as your example above.

>...Max...

In my plan, variables would have same scope in a function as they do now. i.e. variables local to std::accumulate are not visible in the anonymous fn. And yes its not a panacea. For instance, what about static variables in an anonymous function. Do they have the lifetime of the program, or does the anonymous function have a "lifetime" of the calling function?

e.g.

unsigned long foo (vector<X> const &xs)
 unsigned long n = std::accumulate(
       xs.begin(), xs.end(), 0UL,
           unsigned long (unsigned long lhs, X const &rhs) // inline fn with no name.
           {
               static unsigned long y = 0;
               return ++y * (lhs + rhs.count);
           }
       );
   return n;
}

int main()
{
   vector<X> xs;

   // ... initialize xs
   unsigned long a = foo(xs);
   unsigned long b = foo(xs);

   a == b; // depends on the lifetime of foo::anonymous::y

   return 0;
}

In this case perhaps a functor created with a mix of BLL and an anonymous functions would be clear.

  Yours,
  -Gary-


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