I want to achieve the following:
template <typename F>
class MyTemplate {
public:
void SomeMethod(F f) {
//...
}
template <typename C, typename O>
void SomeMethod(C::*F f, O& o) { // this does not work, I want a member function pointer here
//...
}
};
static void StaticFunction(int);
class SomeClass {
public:
void MemberMethod(int);
};
MyTemplate<void(int)> mt;
mt.SomeMethod(StaticFunction);
SomeClass SomeClassInstance;
mt.SomeMethod(&SomeClass::MemberMethod, SomeClassInstance); // I want to call it like this
I want to create a template with a function type parameter. The template should 'create' overloads for C-style or static function pointers as well as members functions. How can I create the member function type from the free function type that should be used to declare the template instance?
I want to be able to use the template with types like:
- void(void)
- void(int)
- int(void)
- int(int)
- int(int, int, int, int)
- void(int, int, int, int)
- etc.
template <typename C, typename O> void SomeMethod(F C::* f, O& o);
Note the placement ofF
. Live example – dypclass
before the declaration ofSomeClass
, no semicolons after class definitions... and there's no declaration ofMyClass
(I guess you meantSomeClass
) – Brian BiF C::* f
? "Pointer to member function of class C returning F". F is void, C is MyClass. – Peter - Reinstate Monica