I want to write a template function that take a std::function as argument, something like:
template<typename T>
void registerDelegate(std::function<void(const T&)> d) {
d(T{});
}
When I want to use it, I need to write in an explicit way the T template argument:
auto d = [](const std::string& message) { int i = 0; };
registerDelegate<std::string>(d);
that's annoying since I've already defined it in the lambda. Is there a way to avoid it and simply write:
registerDelegate(d);
When I want to use the function, while always knowing what T is?
You could forward whatever functor that is supplied to
registerDelegateto a lambda (or function template ) taking astd::function<void(const T&>to deduceT.Instead of changing your current function template which already matches on
std::function<void(const T&)>, you could add a function template for functors/functions not already wrapped up in astd::function: