binding arguments for boost:function
Any thoughts on how to bind some arguments for a function?
Consider the following:
struct A
{
double operator()( const double a, const double b, const double c )
const
{
return a+b+c;
}
};
int main()
{
typedef function
AMDG James Sutherland wrote:
How would I take "f3" and bind one argument? Something like:
F2arg f2 = bind<double>( f3, _2, x2 ); // bind x2 to second argument of f3...
f3 takes 3 arguments, so you need to pass a third argument to bind. F2arg f2 = bind<double>( f3, _1, x2, _2); f2(5, 6) // calls f3(5.0, x2, 6.0) In Christ, Steven Watanabe
On Jul 11, 2008, at 1:52 PM, Steven Watanabe wrote:
f3 takes 3 arguments, so you need to pass a third argument to bind. F2arg f2 = bind<double>( f3, _1, x2, _2);
f2(5, 6) // calls f3(5.0, x2, 6.0)
What if I wanted to do something like: int main() { using namespace boost; using namespace lambda; typedef function< double() > F0arg; const double x1=1.1, x2=2.2; F0arg f = bind( ret<double>(_1*_2)(x1,x2) ); } Or, slightly more generally, if we want to bind other boost::function objects and not POD: typedef function< double(T) > Func; Func func1, func2; ... F0arg f = bind( ret<double>(_1*_2)(func1,func2) ); Any ideas here?
AMDG James Sutherland wrote:
Or, slightly more generally, if we want to bind other boost::function objects and not POD:
typedef function< double(T) > Func; Func func1, func2; ... F0arg f = bind( ret<double>(_1*_2)(func1,func2) );
Any ideas here?
Does this work? using namespace boost::lambda; F0arg f = bind(protect(_1*_2), bind(func1), bind(func2)); In Christ, Steven Watanabe
Does this work? using namespace boost::lambda; F0arg f = bind(protect(_1*_2), bind(func1), bind(func2));
In Christ, Steven Watanabe
I tried the following (the simple case), and it would not compile: int main() { using namespace boost::lambda; typedef boost::function< double() > F0arg; const double x1=1.1, x2=2.2; F0arg f = bind(protect(_1*_2), bind(x1), bind(x2)); }
AMDG James Sutherland wrote:
I tried the following (the simple case), and it would not compile:
<snip>
#include
#include
#include #include int main() { using namespace boost::lambda; typedef boost::function< double() > F0arg; boost::function< double() > arg1, arg2; const double x1=1.1, x2=2.2; F0arg f = bind(protect(_1*_2), x1, x2); boost::function< double() > func1(f), func2(f); F0arg f2 = bind(protect(_1*_2), bind(func1), bind(func2)); }
Any explanation as to why this works and the previous example does not? It seems that the line of code:
boost::function< double() > func1(f), func2(f); is redundant with F0arg f = bind(protect(_1*_2), x1, x2); isn't it? (Clearly not, since it compiles while the earlier version did not)
Jame
It seems that the line of code:
boost::function< double() > func1(f), func2(f); is redundant with F0arg f = bind(protect(_1*_2), x1, x2); isn't it? (Clearly not, since it compiles while the earlier version did not)
Scratch that - temporary insanity. I now understand what you are doing. Thank you! James
I know I am probably trying patience of the readers here, but I have
one more question.
Suppose I have something like
typedef boost::function< double(double) > F1;
F1 f;
F1 g;
and I want to create a function that takes a single argument "t" and
produces f(t)*g(t). I can create the multiply function as
boost::function
James Sutherland:
I can create the multiply function as
boost::function
mult(_1*_2); But now how do I create a "F1" function "h" such that h(t) = f(t)*g(t)?
bind( mult, bind( f, _1 ), bind( g, _1 ) ) creates h(t) = mult( f(t), g(t) ).
I want to do something like:
F1 h = bind( mult, f, g, _1 );
This creates h(t) = mult( f, g, t ).
or
F1 h = bind( mult, bind(f,_1), bind(g,_1), _1 );
This creates h(t) = mult( f(t), g(t), t ). _1 represents the first free argument, 't' in your notation.
participants (3)
-
James Sutherland
-
Peter Dimov
-
Steven Watanabe