我想要一种从函数中制作仿函数的方法.现在我尝试通过lambda函数包装函数调用并稍后实例化它.但编译器说比删除lambda构造函数.那么有什么方法可以编译这段代码吗?或者也许是另一种方式?
#include <iostream>
void func()
{
std::cout << "Hello";
}
auto t = []{ func(); };
typedef decltype(t) functor_type;
template <class F>
void functor_caller()
{
F f;
f();
}
int main()
{
functor_caller<functor_type>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在我得到这样的编译器错误:
error: use of deleted function '<lambda()>::<lambda>()'
error: a lambda closure type has a deleted default constructor
Run Code Online (Sandbox Code Playgroud)
在我看来,唯一的方法是使用宏:
#define WRAP_FUNC(f) \
struct f##_functor \
{ \
template <class... Args > \
auto operator()(Args ... args) ->decltype(f(args...)) \
{ \
return f(args...); \
} \ …Run Code Online (Sandbox Code Playgroud)