本文介绍了在C ++中作为闭包工作的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在C ++中有效地创建一个闭包,它将是一个函数指针?我使用的是Gnu科学图书馆,我必须创建一个。此功能需要在创建时有效关闭几个可用的参数。有一个很好的技巧来创建一个闭包,这样我不必将它们作为params在gsl_function结构中传递?如果不是,我应该传递一个指向包含这些参数的数组的指针?

Is there a way in C++ to effectively create a closure which will be a function pointer? 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
我试图使用boost :: bind like this:

EDITI 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.

// 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;
}

并使用

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

这篇关于在C ++中作为闭包工作的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:17