C++将Lambda函数作为类构造函数参数传递

C++ pass Lambda function as Class constructor argument

本文关键字:构造函数 参数传递 Lambda 函数 C++      更新时间:2023-10-16

我使用的是Visual Studio 2015。我的问题是,当我运行这个程序时,它编译并运行没有问题:

typedef double Fct(double);
struct Function {
Function(Fct f) { cout << f(1) << endl; };
};
double double_func(double x, double n) { return x + n; }
int main() {
for(int n = 0; n < 50; ++n)
Function e{ [](double x) { return double_func(x,1); } }
}

问题是我想要这个部分:

Function e{ [](double x) { return double_func(x,1); } }

有这样一个捕获论点:

typedef double Fct(double);
struct Function {
Function(Fct f) {};
};
double double_func(double x, double n) { return x + n; }
int main() {
for(int n = 0; n < 50; ++n)
Function e{ [n](double x) { return double_func(x,n); } }
}

但我得到了这个错误:no instance of constructor "Function::Function" matches the argument list argument types are: (lambda []double (double x)->double)

编辑:删除示例1。不再工作。:(

Lambdas实际上更像是一个实现了运算符()的类。如果你想保存一个捕获的,你必须将其存储为一个对象函数指针:

int first = 5;
auto lambda = [=](int x, int z) {
return x + z + first;
};
int(decltype(lambda)::*ptr)(int, int)const = &decltype(lambda)::operator();
std::cout << "test = " << (lambda.*ptr)(2, 3) << std::endl;

如果你想返回这个函数并从其他地方执行它。(这在lambdas中实际上是可能的)您必须保存对象:

// OT => Object Type
// RT => Return Type
// A ... => Arguments
template<typename OT, typename RT, typename ... A>
struct lambda_expression {
OT _object;
RT(OT::*_function)(A...)const;
lambda_expression(const OT & object)
: _object(object), _function(&decltype(_object)::operator()) {}
RT operator() (A ... args) const {
return (_object.*_function)(args...);
}
};
auto capture_lambda() {
int first = 5;
auto lambda = [=](int x, int z) {
return x + z + first;
};
return lambda_expression<decltype(lambda), int, int, int>(lambda);
}

Fct不是您试图传递的lambda的超类型。(大概是因为函数指针比这种lambda占用的存储空间更少)您可能希望使用std::function<...>作为Fct的类型,而不是typedef'd函数指针。