简体   繁体   English

C ++编写内联函数以作为参数传递

[英]C++ Writing inline function to pass as argument

I'm writing a set of learning algorithms for a personal library. 我正在为个人图书馆编写一套学习算法。 Learning Algorithms need two things, an input space to explore, and a way to compare one solution against another. 学习算法需要两件事,一个是探索的输入空间,另一个是将一个解决方案与另一个解决方案进行比较的方法。 Defining an input space is the most basic data transfer, however I'm having difficulty with passing it the rating function. 定义输入空间是最基本的数据传输,但是我很难通过它传递评级功能。

Currently my problematic code looks like this: 目前,我有问题的代码如下所示:

RankineGenerator generator(tempHotWater,tempColdWater);

int variablesSize = 2;
Range minMaxVaporizer = { tempColdWater,tempHotWater };
Range minMaxCondenser = { tempColdWater,tempHotWater };
Range minMax[] = {minMaxVaporizer, minMaxCondenser};

auto phi = [&generator] ( double * heatExchangerTemps ) -> double
{
generator.updateHeatExchangeTemps(heatExchangerTemps[0], heatExchangerTemps[1]);
generator.runSim();
return generator.getSpecificWork()/(generator.getColdWaterRelativeMass()+generator.getHotWaterRelativeMass());
};

Twiddle twiddle(variablesSize,phi,minMax,0.001);

And my constructor header looks like: 我的构造函数标头如下所示:

Twiddle(int size, double(*phi)(double*), Range minMax[], double minRatioDeltaPhi);

Where the "phi" lambda function is what I'm trying to pass to to the algorithm "twiddle" 我要传递给算法“ twiddle”的“ phi” lambda函数在哪里

It doesn't necessarily need to be a lambda function, any inline function will do. 它不一定需要是lambda函数,任何内联函数都可以。

Points of concern: 关注点:

Learning algorithms tend to be computation intensive so a fast solution is preferable to an easy solution. 学习算法倾向于计算密集型,因此快速解决方案比简单解决方案更可取。

the function to be passed needs some way of accessing some of the information from the scope it was declared in. 要传递的函数需要某种方式从声明它的作用域中访问某些信息。

The learning algorithm needs to be universal. 学习算法必须是通用的。 Passing it problem specific data isn't an option, I need to be able to plug and play with any problem. 传递问题特定的数据不是一种选择,我需要能够即插即用并解决任何问题。 Ideally the constructor should take a form like: 理想情况下,构造函数应采用如下形式:

constructor( nVars, ratingFunction(vars[nVars]), ranges[nVars], minImprovementSpeed)

Update: It seems I've caused some confusion. 更新:看来我引起了一些混乱。 My apologies. 我很抱歉。

Firstly I'm using the term inline meaning a function written in the middle of a program rather than as an independent entity. 首先,我使用内一词,意思是在程序中间而不是作为独立实体编写的函数。 Not necessarily having anything to do with the keyword of the same name. 不一定与同名关键字有任何关系。

Secondly what I'm trying to do is pass a function with access to some local data. 其次,我想做的是传递具有访问某些本地数据的功能。 For example this: 例如:

double(*phi)(double*) = [](double* heatExchangerTemps) { /*do something*/ };

Twiddle twiddle(variablesSize,phi,minMax,.001);

with construction header: 与施工头:

Twiddle(int size, double (*phi)(double*) , Range minMax[], double minRatioDeltaPhi);

will compile but gives no access to the object "generator". 将编译但不访问对象“生成器”。 However, when I try to capture the "generator" object in the lambda function: 但是,当我尝试在lambda函数中捕获“ generator”对象时:

double(*phi)(double*) = [&generator](double* heatExchangerTemps) { /*do something*/ };

or 要么

auto phi = [&generator] ( double * heatExchangerTemps ) -> double { /*do something*/ };

I get an error. 我得到一个错误。

I could do this: 我可以这样做:

double(*phi)(double*) = [](double* myArray)
{
    Generator generator(/*stuff*/);
    // do something
};

But then a new object of class "Generator" is created every time the function is called. 但是,每次调用该函数时,都会创建一个“ Generator”类的新对象。 Considering this function may be called 1000+ times that's not ideal. 考虑到此功能可能被称为1000次以上,这并不理想。 Plus now the "Twiddle" algorithm needs info specific to the Generator constructor which defeats the whole purpose of having a universal algorithm. 现在,“ Twiddle”算法需要特定于Ge​​nerator构造函数的信息,这不利于拥有通用算法的全部目的。

Ideally I need to be able access a local object (in this case generator) inside the function, and pass the function as a generic argument for a constructor. 理想情况下,我需要能够访问函数内部的本地对象(在本例中为生成器),并将函数作为构造函数的通用参数传递。

Also it doesn't necessarily have to be a lambda function, that's just what first came to mind. 同样,它不一定必须是lambda函数,而这正是刚想到的。

After poking around for a couple hours I found this: 经过几个小时的闲逛,我发现了这一点:

std::function<double(double*)> phi = [&generator] ( double * heatExchangerTemps ) -> double
{ /*do someting*/ };
Twiddle twiddle(variablesSize,phi,minMax,1000000);

with constructor header: 带有构造函数头:

Twiddle(int size, std::function<double(double*)> phi, Range minMax[], double maxResolution);

Not sure if it's the best answer, but it seems to work well. 不知道这是否是最好的答案,但似乎效果很好。

basically my lambda function conforms to the type std::function<double(double*)> which allows me to keep the lambda capture capability while allowing my constructor to understand what it's receiving. 基本上,我的lambda函数符合类型std::function<double(double*)> ,它使我能够保留lambda捕获功能,同时使我的构造函数了解其接收的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM