![](/img/trans.png)
[英]Convert lambda with capture clause stored in std::function to raw function pointer
[英]How to call the lambda stored in pointer to the `std::function`?
我有一个指向std::function
的指针,它在里面存储 lambda。 我怎么能称之为 lambda?
例子:
std::function <void()>* fx = new std::function <void()>([] {std::cout << "Hello world;\n"; });
//here i need to call my fx, just fx(); does not works in this case
fx
可以存储在std::pair
中
您可以通过以下任一方式调用它:
取消引用指针并调用
(*fx)();
或显式调用std::function<R(Args...)>::operator()
fx->operator()(); // explicitly calling the `operator()`
或使用std::invoke
std::invoke(*fx); // needed C++17 or later
但是,您应该重新考虑使用指向std::function
的指针,这通常不是必需的。
fx
可以存储在std::pair
中
这实际上并不重要,因为您需要首先获取对的元素(即pair.first
或pair.second
)并调用存储的 lambda,如上所述。 例如,如果pair.second
是你的fx
(*pair.second)();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.