Boost logo

Boost :

From: Powell, Gary (powellg_at_[hidden])
Date: 2004-03-04 12:05:25


>>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.

> Now, to complicate matters, what if I wish to access v in the
> inner anonymous function? Can you also do that? Example:

> std::for_each(vv.begin(), vv.end(),
> void (vector<int> const& v)
> {
> std::for_each(v.begin(), v.end(),
> void (int x)
> {
> cout << x << endl;
> do_fomething_with(v); // <<--here
> }
> }
> );
> Joel de Guzman

This gets ugly. As you want to "import" only one the variables you declare. For the
previous example with std::accumulate.

e.g.. implementation.

template <class InputIterator,
          class T,
          class BinaryOperation>
T accumulate<InputIterator v,
             InputIterator l,
             T i,
             BinaryOperation bin_op)
{
     while (v != l)
        i = bin_op(i, *v++);
     return i;
}

      int i = 0;
      unsigned long n = std::accumulate(
      xs.begin(), xs.end(), 0UL,
      auto (auto lhs, auto rhs)
      {
           return ( lhs + rhs.count) + i; // Which "i"!!!!
      }

      );

And this is where BLL wins big, it has a mechanism for importing variables into the fn.
And no I don't have a good solution for this.

  -Gary-


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