我正在使用boost :: bind来动态创建组合函数,并希望将对象存储为某个类成员变量以供以后使用.例如,我们有两个仿函数:
struct add{double operator()(double x, double y) const{return x+y;};};
struct multiply{double operator()(double x, double y) const{return x*y;};};
Run Code Online (Sandbox Code Playgroud)
然后创建函数f(x,y,z)=(x + y)*z,我可以这样做:
auto f = boost::bind<double>(multiply(), boost::bind<double>(add(), _1, _2), _3);
Run Code Online (Sandbox Code Playgroud)
调用f(x,y,z)非常有效.现在我想将f保存为类成员变量,如下所示:
struct F
{
auto func;
double operator(const std::vector<double>& args) const
{
return func(args[0],args[1],args[2]); //Skipping boundary check
}
}
F f_obj;
f_obj.func = f;
f_obj(args);
Run Code Online (Sandbox Code Playgroud)
但当然我不能声明一个自动变量.有没有办法解决这个问题?
请注意,我没有使用boost :: function,因为它会极大地影响性能,这对我很重要.
谢谢你的建议.