Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2006-01-11 21:49:53


<snip>

> I want to wrap some existing C code with C++. The function I want to call
back is declareed like this:
> struct X {
>    double foo(double x , void * p);
> };
>
> Idealy I would like to use lambda to remove the void* and replace it with
_1, _2 etc. But I am not sure how > that works with callbacks.

Use boost::bind or boost::lambda::bind. For example:

// An instance of x
X x;

// A binary function
boost::function<double (double, void*)> fBinary;

// A unary function
boost::function<double (void*)> fUnary;

// A nullary function
boost::function<double (void)> fNullary;

// Bind x.foo to binary function
fBinary = boost::bind(&X::foo, boost::ref(x), _1, _2);

// Bind x.foo to unary function "hardcoding" second parameter
fUnary = boost::bind(&X::foo, boost::ref(x), 5.25, _1);

// Bind x.foo to nullary function "hardcoding" first and second parameter
int i = 5;
fNullary = boost::bind(&X::foo, boost::ref(x), 5.25, &i);

// Execute binary function passing both arguments
fBinary(5.25, &i);

// Execute unary function passing 1 argument
fUnary(&i);

// Execute nullary function
fNullary();

Hope this helps,

-delfin


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