Generic function pointer type

I am developing a class that needs to store a call back function. So it needs to include a pointer to some “functor” object and a method to initialize this pointer;


1
2
3
4
5
6
7
Class Thing{
          Funct_t  * callBackptr;
     Public:
          Void setCallBack(Funct_t& foo){
              callBackptr = & foo;
          }
};


My first question is what type should Funct_t be. Conceptually, the signature of the call back function is void(void) but is there a way to define Funct_t generically so that the callBackptr can be set to point to an actually function, a static class method or a lambda expression or anything else that is callable ? Include a lambda with non-empty capture ? (one solution I see is to template Thing and make of Funct_t a template argument but I hope there is a simpler approach.)

More generally, I am wondering if/how pointers to objects such as …

void()()

[]()->void{}

std::function<void()>

… can be cast into each other’s type.
Last edited on
Sounds like you want to use std::function<void()> (not a pointer).
1
2
3
4
5
6
7
class Thing {
	std::function<void()> callback;
public:
	void setCallback(const std::function<void()>& foo) { 
		callback = foo;
	}
};
Topic archived. No new replies allowed.