Boost logo

Boost Users :

Subject: [Boost-users] [lamda] smart self binding of a function object
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-05-11 06:11:42


Hi,

 I usually have function-classes that take two arguments.

class f{
 double operator()(double x, double y) const { x*y+y; }
};

called as:

f myf;
cout << myf(4.,3.);

for some purposes I need a function of only one variable (parametrized
in the first or second argument. So I end up doing:

using boost::function;
using namespace boost::lambda;
function<double(double)> myf_at5 = bind(&f::operator(), myf, 5., _1);
// myf_at5 is myf(5.,*)

So, now I can pass myf_at5 to something that accepts a function of one
variable.

root_finder( myf_at5, ...);

since I have access to f I would like to make f smart enough to handle
its own binding. Eg.

class f{
  ... // same as before
 function<double(double)> operator()(double x_value, YYYY y_arg){
   return bind(
         &f::operator(), //maybe needs cast to double(*)
(double,double)
         *this,
         x_value,
         y_arg
   );
 }
 function<double(double)> operator()(XXXX x_arg, double y_value){
   return bind(
      &f::operator(), //maybe needs cast
      *this,
      x_arg,
      y_value
   );
 }
};

The question is: What should be the type of XXXX and YYYY?

in such a way that I can just call

root_finder( myf(5., _1) , ... );
or
more complicated things like
root_finder( myf(5., _1 * 2.), ...);

in other words,
myf(5.,_1) is the same as the old bind(&f::operator(), myf, 5., _1);

Thank you,
Alfredo
answers using other libraries are also accepted :)


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