C++将Lambda函数作为类构造函数参数传递
C++ pass Lambda function as Class constructor argument
我使用的是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函数指针。
相关文章:
- 在执行new期间是否可以在构造函数中传递不同的参数?
- shared_ptr构造函数参数是否应按值传递
- 如何将 std::string 作为构造函数参数传递,并将其保存的 C 字符串存储在 void 指针中?
- 将派生类构造函数参数传递给受保护的成员
- 在构造函数上传递参数或在每个方法上传递参数
- 如何将变量作为构造函数参数或函数参数传递
- 当数组对象以函数参数传递时,为什么复制构造函数会自称
- 示例自定义共享_ptr-我如何传递构造函数参数 - 我在此处使用variadic模板
- 引用成员到构造函数参数按值传递
- C++将Lambda函数作为类构造函数参数传递
- C :在构造函数中传递指针类参数
- c++在创建时将构造函数参数从父对象传递到子对象
- 在具有boost工厂的构造函数中传递参数
- 在构造函数中传递枚举作为参数
- 将字符串构造作为函数参数传递
- 将构造函数参数传递给模板函数工厂
- 如何删除在 c++ 中作为构造函数参数传递的已分配变量
- 不能正确识别作为构造函数参数传递的函数
- 将方法作为构造函数参数传递
- 为每个可变模板参数调用函数,并将结果作为构造函数参数传递