Boost logo

Boost :

From: Tanton Gibbs (thgibbs_at_[hidden])
Date: 2004-03-08 20:36:56


> So I'm just wondering whether there is something in between the "no
> reference to outside scopes" and full closures, which would hit the
> sweet-spot for C++ better.
>
> Some visible client side syntax would be fine, and probably good,
> to explicitly state what to do with such references (copy, create
> a reference, ...)

I've always been of the opinion that if you allowed the lambdas to curry,
then you could pass in whatever outer level variables you need at the time
of declaration and it would return back a function object representing the
rest of the parameters needed.

For instance:

// loop through vec adding val to each of the elements
int func(std::vector<int>& vec, int val) {
    std::for_each( arr.begin(), arr.end(),
                         lambda (int v, int& elem ) {
                                  elem += v;
                        }( val ) );
}

In the above example, the lambda takes two arguments, an int and an int
reference. Then, we call the lambda with only one argument (the int
parameter). This returns a new lambda that only takes one parameter (an int
reference). With this syntax, you could explicitly pass in the outer level
variables that you wanted to use and it is consistent with other functional
programming languages. Also, this does not carry the overhead associated
with closures or dynamic behavior or anything of the sort.

Thanks!
Tanton


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