Well, you might think that itβs just doing what you are trying to do, but itβs very difficult.
In your case, you want to be able to add a functor, as well as a real function, and this is what complicates the situation. After all, if your class takes a constructor to a Functor functor, where will you store the object?
One method used by boost in its generic pointer class for storing a deleter is to actually have a base class and a derived class. Your constructor will build an object (with a new one), which will be obtained from the base class, and then you will use polymorphism to call.
boost uses all kinds of smart methods in its function and the binder / mpl library, but for now I suggest you use this one.
If you are really against using boost or some kind of general pointer, you will have to manage memory for this object. (Annoying because you want your outer class to be available for copy and assignment.)
I am going to make it simple and say that I can use shared_ptr. I am also going to simplify the signature for now.
Like a circuit ...
template< typename R, typename P > class FunctorBase { public: virtual ~FunctorBase() {} virtual R call(P p) = 0; }; template< typename R, typename P > class FunctionWrapper { shared_ptr< FunctorBase< R, P > > impl; public: template< typename F >
Your functor implementation might look something like this:
template< typename R, typename P, typename F > class FunctorImpl : public FunctorBase< R, P > { F f; public: FunctorImpl( F fparam ) : f( fparam ) { } R call( P p ) { return f( p ); } };
So now you see where you can save the functor.