2

Is there a way to accept any function signature as a parameter? For example:

void accepter(void (*func)(int)) {  ... }

would accept anything of type void (*)(int). I know I could use a template, such as:

template<class T>
void accepter(T func) { ... }

which would work, but then it allows non-functions to be passed in. How do I filter only for functions in the same way that a void * filters for any type of pointer?

sircodesalot
  • 11,231
  • 8
  • 50
  • 83

1 Answers1

2

Using C++11's std::function should work:

#include <functional>

template<typename T>
void test (std::function<T> func) {    

You can use this, e.g.:

int foo (int);
std::sting bar (bool);

test(std::function<int(int)>(foo));
test(std::function<std::string(bool)>(bar));
CodeClown42
  • 11,194
  • 1
  • 32
  • 67