Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2004-03-03 21:41:12


Powell, Gary wrote:

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

I'd asume it can also handle nested anonymous functions? Say, for
example, I wish to print all the contents of a 2D vector-of-vector:

     std::vector<std::vector<int> vv;

     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;
                 }
         }
     );

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
                 }
         }
     );

Cheers,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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