Boost logo

Boost Users :

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


> 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 to binary function
fBinary = boost::bind(&X::foo, boost::ref(x), _1, _2);

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

// Bind 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

// Execute nullary function

Hope this helps,


Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at