The this
keyword can be put by the function you call
class MyClass {
// ...
template<typename Type, typename Func>
void doit(Func f) {
Some::staticFunction<Type>(bind(f, this, _1));
}
};
After which you can call
doit<templateType>(FuncPointer);
If you like you can inherit the function
// T must be a derived class of MyRegister<T>
template<typename T>
class MyRegister {
protected:
template<typename Type, typename Func>
void doit(Func f) {
Some::staticFunction<Type>(bind(f, (T*)this, _1));
}
};
class MyClass : MyRegister<MyClass> {
// ...
};
That way you can just use doit
and not first write it, just like with the macro. Useful if you have a wide range of classes that you use the function in.
Edit: Notice that the C-Style cast is required here (can't use static_cast
), because of the private inheritance. It's a safe cast if T
is derived from MyRegister<T>
Personally i would prefer this over the macro. Notice that your macro can't cope with commas in the type-name
DO(std::pair<A, B>, g);
This tries to pass 3 arguments to the macro instead of 2. You could reverse the order of the type and the function and use variadic macros (which are a C++0x feature, but available in some compiler in C++03 mode) or you could use a typedef and pass the alias name.