RE: [boost] Re: First lambda

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

Powell, Gary wrote:
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.
The basic question is do you bind by value or by reference? int f(std::vector<int> const & v) { int n = 0; for_each( v.begin(), v.end(), void(int const & x) { n += x; } ); return n; } function<int()> g(int n) { return int() { return n; }; }

"Powell, Gary" <powellg@amazon.com> wrote in message news:16D81990DBE42642A8CA977A55FF116A0BD398@ex-mail-sea-02.ant.amazon.com...
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.
That's not what I was asking about. Will the anonymous function see the names visible in the scope of its definition? unsigned long foo (vector<X> const &xs) { unsigned y = 0; unsigned long n = std::accumulate( xs.begin(), xs.end(), 0UL, unsigned long (unsigned long lhs, X const &rhs) // inline fn with no name. { return ++y * (lhs + rhs.count); } ); return n; } A slightly brain-dead example, but it illustrates my question. As I understand it, one can easily include references to local variables in a lambda expression using ref(). Since current language does not allow us to define one function body inside another, we can't really take "as it is now" for a guideline. Nested blocks OTOH do not prevent access to outer local variables, but you can't call nested blocks recursively... ...Max...

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
participants (4)
-
Joel de Guzman
-
Max Motovilov
-
Peter Dimov
-
Powell, Gary