Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2005-03-16 11:28:14


Philippe Després wrote:
> Hello,
>
> I am using Numerical Recipes routines and I am trying to call this function

I wouldn't advise using any of that source code. It's not good C++ code
and the licence fees could get expensive. See
<http://www.accu.org/cgi-bin/accu/rvout.cgi?from=0ti_n&file=n003134a>
for more information.

> void mrqmin(Vec_I_DP &x, Vec_I_DP &y, Vec_I_DP &sig, Vec_IO_DP &a,
> Vec_I_BOOL &ia, Mat_O_DP &covar, Mat_O_DP &alpha, DP &chisq,
> void funcs(const DP, Vec_I_DP &, DP &, Vec_O_DP &), DP &alamda);
>
>
> and I would like to call it with funcs being a member function pointer
> of one of my class. I can declare the member as static but then I
> cannot access member variable, which is the goal here.

You can access static member variables.

> Someone suggested to use boost to solve the problem.
<snip>

Boost doesn't have a solution to this problem. Boost.Bind and
Boost.Function help you to adapt functions, but they produce function
objects that are of no use with a function that requires a function
pointer, such as the C-style code found in Numerical Recipes. Functions
that call-back through function pointers should always pass through an
arbitrary context pointer. This allows you to define a static member
function like this:

void my_class::my_callback(void * context, other args...)
{
     static_cast<my_class *>(context)->real_callback(other args...);
}

and then pass &my_callback and this as arguments to the other function.
  However, it looks like mrqmin doesn't even pass through any context,
so this won't help you. An alternative is to copy this into a static
member variable - or, in a multithreaded application, into TLS - before
making the call. Then you would define your call-back function like this:

void my_class::my_callback(args...)
{
     static_cast<my_class *>(current_instance_)->real_callback(args...);
}

Ben.


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