[lambda] Composing lambda expressions

Given a bit of code like this, struct S { bool pred( ) const; }; typdef vector<pair<string, S *> > V; V :: const_iterator i; i -> second -> pred( ); I'd like to rewrite the last line as a lambda expression. Looking at the documentation, I think this is done with bind( & S :: pred, bind( & V :: value_type :: second, _1 ) )( * i ); which works. But I'd rather be writing something like ( _1 -> second -> pred )( * i ); Am I right in thinking that Boost.Lambda just doesn't work like this, and the frst form is how it's done? Thanks, Rob.

Robert Jones wrote:
Given a bit of code like this,
struct S { bool pred( ) const; };
typdef vector<pair<string, S *> > V; V :: const_iterator i;
i -> second -> pred( );
I'd like to rewrite the last line as a lambda expression. Looking at the documentation, I think this is done with
bind( & S :: pred, bind( & V :: value_type :: second, _1 ) )( * i );
which works. But I'd rather be writing something like
( _1 -> second -> pred )( * i );
Am I right in thinking that Boost.Lambda just doesn't work like this, and the frst form is how it's done?
It doesn't work because Lambda's operator-> won't be able to provide second or pred in its result. You can do this, however: typedef V::value_type VT; bind(&S::pred, &_1->*&VT::second)

On Thu, Jul 17, 2008 at 4:50 PM, Andrey Semashev <andrey.semashev@gmail.com> wrote:
Robert Jones wrote:
Given a bit of code like this,
struct S { bool pred( ) const; };
typdef vector<pair<string, S *> > V; V :: const_iterator i;
i -> second -> pred( );
I'd like to rewrite the last line as a lambda expression. Looking at the documentation, I think this is done with
bind( & S :: pred, bind( & V :: value_type :: second, _1 ) )( * i );
which works. But I'd rather be writing something like
( _1 -> second -> pred )( * i );
Am I right in thinking that Boost.Lambda just doesn't work like this, and the frst form is how it's done?
It doesn't work because Lambda's operator-> won't be able to provide second or pred in its result.
You can do this, however:
typedef V::value_type VT;
bind(&S::pred, &_1->*&VT::second)
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Ah... that 's getting there! And so presumably by extension I can write & ( & _1 ->* & V :: value_type :: second ) ->* & S :: pred; or is that a step too far? -- ACCU - Professionalism in programming - http://www.accu.org

AMDG Robert Jones wrote:
Ah... that 's getting there! And so presumably by extension I can write
& ( & _1 ->* & V :: value_type :: second ) ->* & S :: pred;
or is that a step too far?
I think that you would actually need (untested): bind(&( & _1 ->* & V :: value_type :: second ) ->* & S :: pred) In Christ, Steven Watanabe

On Thu, Jul 17, 2008 at 9:11 PM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
Robert Jones wrote:
Ah... that 's getting there! And so presumably by extension I can write
& ( & _1 ->* & V :: value_type :: second ) ->* & S :: pred;
or is that a step too far?
I think that you would actually need (untested):
bind(&( & _1 ->* & V :: value_type :: second ) ->* & S :: pred)
It's minefield! I too am away from a compiler - I'll give a few options a whirl when I'm back in the office. Thanks, Rob.
participants (3)
-
Andrey Semashev
-
Robert Jones
-
Steven Watanabe