Boost logo

Boost Users :

From: Chris Weed (chrisweed_at_[hidden])
Date: 2006-10-19 08:17:39


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_at_[hidden]> 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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net