Inheritance is really the only way to do this generally.
Code:
template <typename InType, typename OutType>
class generic_functor
{
public:
virtual ~generic_functor() {}
virtual OutType operator()(const InType &val) = 0;
};
template <typename InType, typename OutType, typename Func>
class concrete_functor : public generic_functor<InType, OutType>
{
public:
concrete_functor(Func func)
: function(func)
{}
OutType operator()(const InType &val)
{
return function(val);
}
private:
Func function;
};
Now you store a pointer to a generic_functor<I, O> which points to some concrete_functor. You would use a templatized constructor to initialize it:
Code:
template <typename InType, typename OutType>
class some_container
{
public:
template <typename Func>
some_container(..., Func func)
: functor(new concrete_functor<InType, OutType, Func>(func))
{}
~some_container()
{
delete functor;
}
private:
generic_functor<InType, OutType> *functor;
};
You might avoid needing the destructor in some_container by using some kind of smart pointer to point at the generic_functor