使用void Pointers(Cuba Nint常规)时施放功能
Casting a function when using void pointers (CUBA Nint routine)
老实说,我不是一个好程序员。我的任务是更改与古巴库的数值集成程序。但这不一定重要。我得到的是一个定制的例程,其中我具有许多可以集成,定义的功能:
float spectra(float x[],float)
传递给数字维加斯例程
float integrate ( float (*fxn)(float[],float), unsigned long ncall, int itmx )
致电
integrate(spectra,ncall,itmx);
然后将其传递到实际例程。这一切都很好。
现在,在古巴例程中,我与:
进行集成Cuhre(NDIM, NCOMP, Integrand, USERDATA, NVEC, ... ,integral, ...);
重要的是intemandand是函数,而用户达塔是一个空隙指针,可以传递您需要集成的任何额外内容。有点像GSL例程。
我不想重写所有"光谱"功能以使古巴例程具有适当的形式。我只想在中间包装。
Integrand具有铸件:
static int Integrand(const int *ndim, const cubareal xx[], const int *ncomp, cubareal ff[], void *userdata);
wher xx []是输入,(目前只有1个),ff []是输出(也是1)。UserData是我需要打字的空隙指针。
我的目标是制作一个包装器,以使程序中的任何地方都在调用:集成(功能,...)我可以按照我的意愿替换IntegrateCuba(功能,...)
我的尝试是将Integrand函数定义为一个空白模板,该模板将传递给UserData,该模板是输入到此float函数的类型铸造,然后从那里开始。我在下面的声明中说什么?
static int Integrand(const int *ndim, const cubareal xx[], const int *ncomp, cubareal ff[], void *userdata) {
#define f ff[0]
float xxx[ndim]; //xx is inputs, ff is outputs
xxx[0] = xx[0]; //ndim is 1 for now.
//here I try to cast this void pointer
float (*fxn)(float[],float) = NULL;
// What do I put here??????
*fxn = ???
f = fxn(xxx,0.0);
return f ;
}
最后,我将有一个例程:
float integrateCUBA ( float (*fxn)(float[],float), unsigned long ncall, int itmx )
{
int comp, nregions, neval, fail;
cubareal integral[NCOMP], error[NCOMP], prob[NCOMP];
Cuhre(NDIM, NCOMP, Integrand, &fxn, NVEC, ...
我通过用户数据指针发送的地方"& fxn"。
我希望我很清楚。我要做的是,我要做的是将指针传递到函数" float(*fxn)(float [],float)",然后通过空隙指针进行正确的键入。
。此代码是用C/C 编写的(我以外的其他人来自一堆旧的科学代码的可怕的混蛋。)它将在C 11中编译,因此,如果有一种简单的静态铸造方法那个也是。
非常感谢您的任何建议!
您应该只能施放,但是语法在不可读方面:
float (*fxn)(float[],float) = (float (*)(float[],float)) userdata;
// or static_cast<float (*)(float[],float)>(userdata);
f = fxn(xxx,0.0);
使用Typedef可以提高可读性:
typedef float (*Function)(float[],float);
Function fxn = (Function) userdata;
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 正确施放以指向返回功能的函数的功能指针
- 当功能采用签名的int时,将浮点参数施放为未符号
- 呼叫功能时防止int施放
- ToString功能或(STD :: String)在C 中施放过载
- 为什么不能从指针施放到模板功能中的指针
- 使用void Pointers(Cuba Nint常规)时施放功能