开发者

Function pointers working as closures in C++

Is there a way in C++ to effectively create a closure which will be a function poi开发者_如何学Cnter? I am using the Gnu Scientific Library and I have to create a gsl_function. This function needs to effectively "close" a couple of parameters available when I create it. Is there a nice trick to create a closure so that I don't have to pass all of them as params in the gsl_function structure? If not, should I just pass in a pointer to an array containing these parameters?

EDIT I have tried to use boost::bind like this:

#include <gsl/gsl_integration.h>
#include <boost/bind.hpp>

#include "bondpricecalculator.h"
#include "functions.h"

double integrand (double xi, double t, double x, void * p) {
        Functions *functions = (Functions *) p;
        double vx = functions->v(x);
        return functions->rho0(x)*exp(vx * xi - 0.5 * vx * vx * t);
     }

double BondPriceCalculator::value(double t, double T, double xi)
{
    gsl_integration_workspace * w
         = gsl_integration_workspace_alloc (10000);

    gsl_function F;

    F.function = &boost::bind(integrand, xi, t, _1, _2);
    F.params = &functions;

    double integral_t;
    double integral_T;
    double error;

    int res = gsl_integration_qags(&F, T, 1e+14, 0, 1e-7, 10000, w, &integral_T, &error);
    if(res)
    {
        throw "Error intgrating";
    }

    int res = gsl_integration_qags(&F, T, 1e+14, 0, 1e-7, 10000, w, &integral_t, &error);
    if(res)
    {
        throw "Error intgrating";
    }

    return integral_T/integral_t;
}

but I got the following error message:

/home/ga/svn/PhD/inflation/cpp/ioi/bondpricecalculator.cpp:20: error: cannot convert ‘boost::_bi::bind_t<double, double (*)(double, double, double, void*), boost::_bi::list4<boost::_bi::value<double>, boost::_bi::value<double>, boost::arg<1>, boost::arg<2> > >*’ to ‘double (*)(double, void*)’ in assignment


I found below code at.

http://bytes.com/topic/c/answers/657124-interface-problem

// Use in combination with boost::bind.
template<class F>
static double gslFunctionAdapter( double x, void* p)
{
    // Here I do recover the "right" pointer, safer to use static_cast
    // than reinterpret_cast.
        F* function = static_cast<F*>( p );
    return (*function)( x );
}

template<class F>
gsl_function convertToGslFunction( const F& f )
{
    gsl_function gslFunction;

    const void* p = &f;
    assert (p != 0);

    gslFunction.function = &gslFunctionAdapter<F>;
    // Just to eliminate the const.
    gslFunction.params = const_cast<void*>( p ); 

        return gslFunction;
}

and use this like

gslFunction gslF = convertToGslFunction( boost::bind( &Sde::drift, &sde, _1 ) );


Take a look at this simple example of combining boost::bind and boost::function.


I'm guessing from all those "gsl_" prefixes that the library is not C++, but plain C. Which means it doesn't grok C++ closures (functors). You can't pass a C++ functor to a C function. You'll have to pass void pointers around, cross your fingers and reinterpret_cast them into C oblivion.


Though bradgonesurfing has given a nice answer that will work for converting closures into gsl_functions without any further thought, I would like to share with you the idiom for doing a direct translation from C++ into C.

Supposing you have the closure:

double a;
[&a](double x){return a+x;}

You would convert translate this into an equivalent function pointer idiom as follows:

struct paramsAPlusX{
    double* a;
    paramsAPlusX(double & a_):a(&a_){}
}
double funcAPlusX(double x, void* params){
   paramsAPlusX* p= (paramsAPlusX*)params;
   return *(p->a) + x;
}

//calling code:
double a;
paramsAPlusX params(a);
gsl_function f;
f.function=funcAPlusX;
f.params=&paramsAPlusX;
//use f here.

Many C libraries use this sort of idiom, and they don't all use a struct for it (they frequently pass it as two separate parameters to the function) so automatic conversion isn't always possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜