Using Bind & Lambda

This is my first time posting to this list, so I hope you'll forgive me it my etiquette is awry. I'm new to the Boost libraries, and just learning my way around, guided mainly by Bjorn Karlson's book - any other book recommendations would be most welcome btw, and there a few things I just don't quite get! First - is "boost/lambda/bind.hpp" equivalent to "boost/bind.hpp"? Should it ever be necessary to include both? Second - Is it true that a lambda function is syntactically and semantically the same as a bind finction, eg Given:- int foo( int i ) { return i + 3; } Are these two expressions the same? bind( &foo, _1 ); _1 + 3; Finally (for now!) - why is this wrong? struct A { string identity( string ) const; } a; vector<string> v; for_each( v.begin(), v.end(), cout << bind( &A::identity, a, _1 ) << '\n' ); which is intended to output the return value of A::identity() for each element of v, separated by newlines. Thanks in advance for any guidance! Rob. -- ACCU - Professionalism in programming - http://www.accu.org

AMDG Robert Jones wrote:
First - is "boost/lambda/bind.hpp" equivalent to "boost/bind.hpp"? Should it ever be necessary to include both?
Bind and Lambda are separate. If you are using Lambda you need to use boost/lambda/bind.hpp which contains boost::lambda::bind.
Second - Is it true that a lambda function is syntactically and semantically the same as a bind finction, eg
Given:- int foo( int i ) { return i + 3; }
Are these two expressions the same? bind( &foo, _1 ); _1 + 3;
They should behave the same, unless this is a boost::bind and you want to do further composition.
Finally (for now!) - why is this wrong?
struct A { string identity( string ) const; } a;
vector<string> v; for_each( v.begin(), v.end(), cout << bind( &A::identity, a, _1 ) << '\n' );
which is intended to output the return value of A::identity() for each element of v, separated by newlines.
It will only work if you using boost::lambda::bind, not boost::bind. In Christ, Steven Watanabe

When should be used boost/lamda/bind.hpp? Is there somewhere a documentation which compares the 2 implementations? Best regards Hansjörg Steven Watanabe schrieb:
AMDG
Robert Jones wrote:
First - is "boost/lambda/bind.hpp" equivalent to "boost/bind.hpp"? Should it ever be necessary to include both?
Bind and Lambda are separate. If you are using Lambda you need to use boost/lambda/bind.hpp which contains boost::lambda::bind.
Second - Is it true that a lambda function is syntactically and semantically the same as a bind finction, eg
Given:- int foo( int i ) { return i + 3; }
Are these two expressions the same? bind( &foo, _1 ); _1 + 3;
They should behave the same, unless this is a boost::bind and you want to do further composition.
Finally (for now!) - why is this wrong?
struct A { string identity( string ) const; } a;
vector<string> v; for_each( v.begin(), v.end(), cout << bind( &A::identity, a, _1 ) << '\n' );
which is intended to output the return value of A::identity() for each element of v, separated by newlines.
It will only work if you using boost::lambda::bind, not boost::bind.
In Christ, Steven Watanabe
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Tue, Jun 10, 2008 at 09:46, Hansi <hansipet@web.de> wrote:
When should be used boost/lamda/bind.hpp? Is there somewhere a documentation which compares the 2 implementations?
AFAIK, the only reason to use Boost.Bind instead of Boost.Lambda is that it supports more platforms. But once you've picked one, you need to remember to use it consistently in an expression. Bind's _1 won't work with Lambda's bind, and vice versa, as was mentioned. Of course, nothing prevents you from using both if they're in different parts. (Though you might pay a bit in executable size, relative to using just Lambda consistently.) HTH, ~ Scott

Scott McMurray wrote:
On Tue, Jun 10, 2008 at 09:46, Hansi <hansipet@web.de> wrote:
When should be used boost/lamda/bind.hpp? Is there somewhere a documentation which compares the 2 implementations?
AFAIK, the only reason to use Boost.Bind instead of Boost.Lambda is that it supports more platforms.
For me, the most common reason for using Boost.Bind is that it supports boost::shared_ptr<>. FWIW / Johan
participants (5)
-
Hansi
-
Johan Nilsson
-
Robert Jones
-
Scott McMurray
-
Steven Watanabe