[function][lambda] Create a boost function to a bind functor

Hi, I would like to create a boost::function to a lambda::bind functor such as float fx(float a, float b) { return a+b; } int main() { boost::function<float(float)> f = boost::lambda::bind(fx,_2,2); std::cout << f(3) << std::endl; return 0; } I would expect this to bind 2 to the "b" argument of fx, and print "5". Instead with msvc-8.0 I get a bunch or compile errors. Any help would be greatly appreciated. Chris

a) You want to use _1 instead of _2 b) _1/_2 are in namespace boost::lambda boost::function<float(float)> f = boost::lambda::bind(fx,boost::lambda::_1,2); It is actually possible to use _2 but then the functor takes to args and ignores the first: boost::function<float(float,float)> f = boost::lambda::bind(fx,boost::lambda::_2,2); std::cout << f(100,3) << std::endl; // returns 5 cheers Arnaldur "Chris Weed" <chrisweed@gmail.com> Sent by: boost-users-bounces@lists.boost.org 19.10.2006 05:04 Please respond to boost-users@lists.boost.org To boost-users@lists.boost.org cc Subject [Boost-users] [function][lambda] Create a boost function to a bind functor Hi, I would like to create a boost::function to a lambda::bind functor such as float fx(float a, float b) { return a+b; } int main() { boost::function<float(float)> f = boost::lambda::bind(fx,_2,2); std::cout << f(3) << std::endl; return 0; } I would expect this to bind 2 to the "b" argument of fx, and print "5". Instead with msvc-8.0 I get a bunch or compile errors. Any help would be greatly appreciated. Chris _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi, I can get the following code to compile and it prints 3 2 5 but I don't quite understand why it doesn't print 2 3 5 or how to get it to do so. I thought since it is binding 2 to _1, this would bind it to the a argument. #include <boost/function.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <iostream> float fx(float a, float b) { std::cout << a << " " << b << std::endl; return a+b; } int main() { boost::function<float(float)> f = boost::lambda::bind(fx,boost::lambda::_1,2); std::cout << f(3) << std::endl; return 0; } Thanks, Chris On 10/19/06, Arnaldur Gylfason <arnaldur.gylfason@decode.is> wrote:
a) You want to use _1 instead of _2 b) _1/_2 are in namespace boost::lambda boost::function<float(float)> f = boost::lambda::bind(fx,boost::lambda::_1,2);
It is actually possible to use _2 but then the functor takes to args and ignores the first: boost::function<float(float,float)> f = boost::lambda::bind(fx,boost::lambda::_2,2); std::cout << f(100,3) << std::endl; // returns 5
cheers
Arnaldur

Chris Weed wrote:
Hi, I can get the following code to compile and it prints 3 2 5 but I don't quite understand why it doesn't print 2 3 5 or how to get it to do so. I thought since it is binding 2 to _1, this would bind it to the a argument.
Calling bind( fx, _1, 2 ) with 3 will put the '3' in the _1 and pass the 2 intact, the result is fx( 3, 2 ). bind( fx, _1, _1 ) will call fx( 3, 3 ). bind( fx, 2, _1 ) calls fx( 2, 3 ). bind( fx, 4, 5 ) calls fx( 4, 5 ). Does it make sense now? bind is not an interpreter with a mini-language ("bind the second argument to 2"), you just pass the argument values, and _1 is substituted with the first input argument (3 in your case).

Yes, That clears up my understanding. Thanks, Chris On 10/19/06, Peter Dimov <pdimov@mmltd.net> wrote:
Chris Weed wrote:
Hi, I can get the following code to compile and it prints 3 2 5 but I don't quite understand why it doesn't print 2 3 5 or how to get it to do so. I thought since it is binding 2 to _1, this would bind it to the a argument.
Calling bind( fx, _1, 2 ) with 3 will put the '3' in the _1 and pass the 2 intact, the result is fx( 3, 2 ). bind( fx, _1, _1 ) will call fx( 3, 3 ). bind( fx, 2, _1 ) calls fx( 2, 3 ). bind( fx, 4, 5 ) calls fx( 4, 5 ).
Does it make sense now? bind is not an interpreter with a mini-language ("bind the second argument to 2"), you just pass the argument values, and _1 is substituted with the first input argument (3 in your case).
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

It is binding 2 to the second argument of the function (which is b) but _1 to a which means the argument you call f with is passed as a to your function. Hence f(3) prints 3 2 and returns 5. If you want 2 3 you need: boost::function<float(float)> f = boost::lambda::bind(fx,2,boost::lambda::_1); cheers Arnaldur "Chris Weed" <chrisweed@gmail.com> Sent by: boost-users-bounces@lists.boost.org 19.10.2006 12:18 Please respond to boost-users@lists.boost.org To boost-users@lists.boost.org cc Subject Re: [Boost-users] [function][lambda] Create a boost function to a bind functor Hi, I can get the following code to compile and it prints 3 2 5 but I don't quite understand why it doesn't print 2 3 5 or how to get it to do so. I thought since it is binding 2 to _1, this would bind it to the a argument. #include <boost/function.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <iostream> float fx(float a, float b) { std::cout << a << " " << b << std::endl; return a+b; } int main() { boost::function<float(float)> f = boost::lambda::bind(fx,boost::lambda::_1,2); std::cout << f(3) << std::endl; return 0; } Thanks, Chris On 10/19/06, Arnaldur Gylfason <arnaldur.gylfason@decode.is> wrote:
a) You want to use _1 instead of _2 b) _1/_2 are in namespace boost::lambda boost::function<float(float)> f =
boost::lambda::bind(fx,boost::lambda::_1,2);
It is actually possible to use _2 but then the functor takes to args and
ignores the first:
boost::function<float(float,float)> f = boost::lambda::bind(fx,boost::lambda::_2,2); std::cout << f(100,3) << std::endl; // returns 5
cheers
Arnaldur
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Arnaldur Gylfason
-
Chris Weed
-
Peter Dimov