C++11 C++;:标准::绑定->;std::函数

C++11 C++;:标准::绑定->;std::函数,c++11,functional-programming,bind,C++11,Functional Programming,Bind,我有几个功能,接收以下类型: function<double(int,int,array2D<vector<double *>>*)> 现在,为了绑定第一个值,temp,并返回具有正确签名的函子,我写下: double temp = some_value; function<double(int,int,array2D<vector<double *>>*)> step_func = [temp](int i,

我有几个功能,接收以下类型:

function<double(int,int,array2D<vector<double *>>*)>
现在,为了绑定第一个值,
temp
,并返回具有正确签名的函子,我写下:

double temp = some_value;
function<double(int,int,array2D<vector<double *>>*)> step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){
        return ising_step_distribution(temp,i,j,model);
    }
}
double temp=某个_值;
函数步骤_func=
[temp](内部i、内部j、阵列2D*型号){
返回伊辛阶跃分布(温度、i、j、型号);
}
}
这是有效的。但是,以下中断:

auto step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){
        return ising_step_distribution(temp,i,j,model);
    }
}
自动步进函数=
[temp](内部i、内部j、阵列2D*型号){
返回伊辛阶跃分布(温度、i、j、型号);
}
}
出现以下错误:

candidate template ignored: 
could not match 
'function<double (int, int, array2D<vector<type-parameter-0-0 *, allocator<type-parameter-0-0 *> > > *)>' 
against 
'(lambda at /Users/cdonlan/home/mcmc/main.cpp:200:25)'
void mix_2D_model(function<double(int,int,array2D<vector<T*>>*)> step_distribution_func,...
已忽略候选模板:
无法匹配
“功能”
反对
“(lambda at/Users/cdonlan/home/mcmc/main.cpp:200:25)”
空隙混合二维模型(函数阶跃分布函数),。。。
因此,代码块是丑陋的、模糊的和重复的(因为我正在制作许多这样的代码)


我一直在阅读文档,我理解我应该能够写:

function<double(int,int,array2D<vector<double *>>*)> step_func = 
    bind(ising_step_distribution,temp,_1,_2,_3);
函数步骤\u func=
绑定(伊辛步分布、温度、1、2、3);
但是,我所看到的示例仅适用于
function
类型的函数。此示例失败并出现错误:

// cannot cast a bind of type 
// double(&)(double,int,int,array2D<vector<double *>>*) 
// as function<double(int,int,...)
//无法强制转换类型为的绑定
//双精度(&)(双精度,整数,整数,数组2d*)
//作为功能
如何获得视觉上干净的绑定和投射

一种方法是:

using F = function<double(int,int,array2D<vector<double *>>*)>;
auto step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){
        return ising_step_distribution(temp,i,j,model);
    }
}
或:


第二个lambda出现了什么错误?lambda几乎总是首选
std::bind
;如果可能的话,我会尝试让它与lambda版本一起工作。@0x5453啊,好的。一秒钟后,我将重新运行it@0x5453出现错误。看起来不确定
array2D*
中的内部类型是什么
auto step\u func=版本似乎缺少分号此处没有足够的信息来重现lambda编译器错误..请生成一个非常棒的--外观,视觉上,与我在主方法中所要的相似。您是否有时间解释using在此处所做的工作?无论是哪种方式,谢谢--accepted@bordeo它使用符号。基本上,调用
F
constructor将lambda传递给它。它正在进行构造转换。这是一种无害的转换,但您必须考虑您的内部转换规则。考虑将此处的“使用”作为typedef的一种新形式,以使模板更简单。
using F = function<double(int,int,array2D<vector<double *>>*)>;
auto step_func = 
    [temp](int i, int j, array2D<vector<double *>>* model){
        return ising_step_distribution(temp,i,j,model);
    }
}
auto step_func_2 = F(step_func);
mix_2D_model(step_func_2, ...);
mix_2D_model(F(step_func), ...);