Reputation: 1080
I want to use boost::bind (I'm not sure if it is really the right solution to my problem) to create a wrapper to a function that takes one or two arguments, while the wrapper takes only one argument and fixes the other to some constant value. My functions look like
double f(double a, double b)
{
return a/b;
}
or
double g(double b)
{
return 2*b; // b+b?
}
In my code I have a typedef for a function with one argument:
typedef boost::function<double (double)> callback;
and my idea was to create the function wrapper with this:
callback cb;
cb = boost::bind(f, _1, 2)(x);
so that I could call a third function that uses a wrapped function passed in the arguments:
double use(callback cb, double x, double y)
{
return cb(x0) - y0;
}
I have about 30 functions with one or two arguments, and the first or the second must be a constant. This constant is not known to use()
, but the algorithm implemented in use
works with every function.
When I try to compile my code, I get an error for the line cb = boost::bind(f, _1, 2)(x);
:
'* f' cannot be used as a function
When I try to use the bind directly, as in cout << boost::bind(f, _1, 2)(x);
, I don't get an error.
What have I done wrong here?
Upvotes: 0
Views: 1136
Reputation: 523304
Why add the (x)
?
cb = boost::bind(f, _1, 2);
This already defines a wrapper of f
which the second argument is 2, and you can call
cb(x0)
to get f(x0, 2)
.
Upvotes: 2