boost::function, boost::bind question
Hello,
I am using Numerical Recipes routines and I am trying to call this function
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. Someone suggested
to use boost to solve the problem. His solution is here
http://www.numerical-recipes.com/forum/showthread.php?s=&threadid=526
I tried to implement this but the result is this
error: cannot convert `boost::function
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
ok thanks a lot. I do not like NR either but I did not find a good C++ alternative yet... Ben Hutchings wrote:
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
(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
(current_instance_)->real_callback(args...); } Ben.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Philippe Després Associate Specialist UCSF Physics Research Lab 185 Berry Street suite 350 San Francisco CA 94107 (415) 353-4501 (office) (650) 497-2261 (home) pdespres@radiology.ucsf.edu PGP PUBLIC KEY http://itsa.ucsf.edu/~despres/publickey.txt
participants (2)
-
Ben Hutchings
-
Philippe Després