1

I can pass a lambda or a function pointer to a parameter expecting a specific std::function instantiation as shown:

#include <functional>

void foo1(std::function<void()> f) { f(); }
void bar() { }

void test1()
{
  foo1([](){});
  foo1(bar);
}

If instead any of the std::function template parameters are left unspecified, providing either such argument type fails to compile:

template <typename T>    
void foo2(std::function<T()>    f) { f(); }

void test2()
{
  foo2([](){}); // error
  foo2(bar);    // error
}

Can I work with generic std::function parameters in such a scenario? Or should I switch to using function pointer parameters? I won't always know the types or arity of the arguments in advance, but the lambdas will always be captureless.

user2023370
  • 10,488
  • 6
  • 50
  • 83

0 Answers0