I have the following class:
template<typename R, typename... Args>
class Callable final
{
public:
Callable(void* args, R(*fn)(Args...));
Callable(void* args, std::function<R(Args...)> &&fn);
/* parse args into a tuple<Args...> and invoke callable */
void* invoke();
private:
void* args;
std::function<R(Args...)> callable;
};
and I can use it like:
void* test(void* a, const char* b, const char* c)
{
return nullptr;
}
Callable(args, test).invoke();
Callable(args, std::function([](void* a, void* b, void* c){})).invoke();
but it doesn't allow me to do:
Callable(args, [](void* a, void* b, void* c){}).invoke();
//No viable constructor or deduction guide for deduction of template arguments of 'Callable'
I must wrap the lambda
in an std::function
. Is there a way to allow my class to accept a lambda directly and store it as an std::function
without having to explicitly specify std::function(lambda)
as a constructor parameter?
I don't want to do Callable(args, std::function([](void* a, void* b, void* c){})).invoke();
which explicitly wraps the lambda in a function. I want to pass the lambda directly and let the constructor store it as a function internally.
How can I do that?