I want a way to make functor from function. Now I trying to wrap function call by lambda function and instantiate it later. But compiler says than lambda constructor is deleted. So is there any way to compile this code ? Or maybe another way for that ?
#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;
}
Now I get such compiler error:
error: use of deleted function '<lambda()>::<lambda>()'
error: a lambda closure type has a deleted default constructor
In my opinion the only way is to use macro:
#define WRAP_FUNC(f) \
struct f##_functor \
{ \
template <class... Args > \
auto operator()(Args ... args) ->decltype(f(args...)) \
{ \
return f(args...); \
} \
};
then
WRAP_FUNC(func);
and then (in main)
functor_caller<func_functor>()