Subject: Re: phoenix lambda-lambda for_each example
From: Joel de Guzman <[email-masked]>
To: Aleksey Gurtovoy <[email-masked]>
Cc: David Abrahams <[email-masked]>, Noah Stein <[email-masked]>, [email-masked], Jaakko Jarvi <[email-masked]>
Date: Wed, 03 Mar 2004 12:48:52 +0800

Aleksey Gurtovoy wrote:

> Joel de Guzman writes:
> 
>>Aleksey Gurtovoy wrote:
>>
>>>What if I don't want the scope there?
>>>    std::vector<int> v1, v2;
>>>    copy( arg1, back_inserter(arg2) )(v1, v2); // fails???
>>
>>No, that's ok! Why? because copy does not require a lambda-lambda.
>>The signature of for_each is: for_each(c, lambda) while the signature
>>of copy is simply copy(c, c).
> 
> 
> OK, 'copy' is not very interesting then. Hopefully this one
> demonstrates my question better:
> 
>     for_each( v1, push_back(arg1, arg1) )( v2 ); // works?
>     for_each( arg1, push_back(arg2, arg1) )( v1, v2 ); // doesn't, right?
> 
> Actually, the latter is not achievable even with explicit scoping, so
> I guess it's not the question of explicit vs. implicit scopes, but
> rather how to override the default behavior.

I encountered this problem with phoenix's local vars too.
The solution I had was to use outer(arg1) to refer to the
outer scope:

     for_each(arg1, push_back(outer(arg2), arg1) )( v1, v2 );

I am also contemplating on incorporating FC++ style scopes
as an optional scheme:

     lambda(seq)
     [
         for_each(seq,
             lambda(elem)
             [
                 push_back(seq, elem)
             ]
         )
     ]
     (v1, v2);

but I find it too noisy for my taste ;-) Comments?

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