Consider the following class:
class MyClass { public: template<class T> typename T::result_type apply(T& func) { if (is_int()) { return func(int(0)); } return func(double(0)); } ... };
(The code doesn't look terribly useful, but it's just a contrived sample to demonstrate my point)
Anyway, a typical functor would be something like this:
struct MyFunc { typedef void result_type; template<class V> void operator()(V) {
And you can use it like this:
MyClass c; MyFunc f; c.apply(f);
My question is this: is it possible to modify MyClass::apply to recognize a slightly different version of the functor in addition to the original one , for example, one that expects a reference to the object of the calling object to be passed along with all other parameters, something like this:
struct MyFuncEx { typedef void result_type; template<class V> void operator()(const MyClass& caller, V) {
So, the following code compiled too:
MyClass c; MyFunc f; c.apply(f); MyFuncEx f2; c.apply(f2);
As a bonus, I would like the compilation to fail if the functor contains both overloads, i.e. The following should complete with compilation:
struct MyFuncSmartAss { typedef void result_type; template<class V> void operator()(V) {
But this is not so important if a longer overload takes precedence over a shorter one.