Use the interface and derived template class. This method is called type erasure.
class CallbackBase { public: virtual ~CallbackBase() = 0; virtual bool exec(const RequestObject& request, ResponseRequest& response) = 0; }; template <typename F> class CallbackImpl : public CallbackBase { public: CallbackImpl(F f) : f_(f) {} virtual bool exec(const RequestObject& request, ResponseRequest& response) { return f_(request, response); } private: F f_; }; template <typename F> void CreateCallback(F f, std::auto_ptr<CallbackBase>& r) { r.reset(new CallbackImpl<F>(f)); }
You might want to use shared_ptr or the like instead of auto_ptr, it all depends on how you want to save these objects.
Edit: you can write your own object-object-wrapper / closure / function object. The code looks something like this (I did not try to compile it so that there might be some errors):
template <typename T> class RequestWrapper { typedef bool (T::*F)(const RequestObject&, ResponseRequest&); T* obj_; F f_; public: RequestWrapper(T* obj, F f) : obj_(obj) , f_(f) {} bool operator()(const RequestObject& request, ResponseRequest& response) const { return (obj_->*f_)(request, response); } }; template <typename T, typename F> RequestWrapper<T> BindRequestMfn(T* obj, F mfn) { return RequestWrapper<T>(obj, mfn); } CreateCallback(BindRequestMfn(foo, &FooResource::fooBarRequest), my_callback);
source share