Hi,

instead of writing this

SomeFunction<Class>::Bind<&Class::MemberFunction>(this);

I want to provide an easier way to call "SomeFunction".

I have one solution using a macro like this:

GET_EXAMPLE(Class, &Class::MemberFunction, this);
//...
#define GET_EXAMPLE(ClassName, MemberFunctionPointer, InstancePointer) \
  (SomeFunction<ClassName>::Bind<MemberFunctionPointer>(InstancePointer))

But in order to avoid the use of this macro I'd like to do something like the following. The problem is that I get the error: template parameter 'T_MEMBER_FUNCTION' : 'MemberFunctionPointer' : a local variable cannot be used as a non-type argument

//...
GetExample(&Class::MemberFunction, this));
//...
template<class T_CLASS>
inline static void GetExample(void (T_CLASS::*MemberFunctionPointer)(), T_CLASS* InstancePointer)
{
  SomeFunction<T_CLASS>::Bind<MemberFunctionPointer>(InstancePointer);
}

Any solutions to this problem?

Thanks,
Mirco

Hi,

Why you want to bind member function type with a static bind method? you wont be able to call it without having proper address of class member function.

Anyway, I hope following code helps, let me know if it works?

#include <iostream>
using namespace std;


template <typename T>
class SomeFunction{
        T instance;
public:
        template <typename FunPtr>
        void Bind(T _instance, FunPtr funPtr){
                //Bind your funPtr here.
                //use a place holder to hold the FunPtr type as you will want to use it
        }
};

template <typename T, typename FunPtr> //Let argument deduction find out the type.
void Function(T instance, FunPtr funPtr){
        (instance.*funPtr)();
}

class AClass{
public:
        void ClassFun(){
                cout << "Class is having fun" << endl;
        }
};

int main() {

        AClass x;
        Function(x,&AClass::ClassFun);

        return 0;
}

Can yo explain what exactly your bind method will do and what you want to do after binding the type? then may be i could give more proper example.

Edited 5 Years Ago by alwaysLearning0: n/a

Hi,

the Bind-method calls the constructor of a class CDelegate which instantiates objects that store a function pointer and an object pointer. I use this mechanism to register ISRs in an embedded system.
Everything works fine, I just want to provide an easier way to call the Bind-method in form of avoiding the use of <>.

What I want is passing a function pointer as paramater to a wrapper function. In this wrapper function I want to use this function pointer as a non-type template parameter and pass it to the Bind-method. As I figured out it is not possible to first pass it as a standard function parameter and then use it as a template parameter because in this case it will not be known to the compiler at compile time...

Yea, you are right, you wont get the address of the function compile time.

I just want to provide an easier way to call the Bind-method in form of avoiding the use of <>.

Doesn't argument deduction shown on my last post help?

In your post in Function() you call ClassFun() directly but I need the function pointer in Function() to again pass it as a non-type template parameter. I can't do this with local variables I guess.

This article has been dead for over six months. Start a new discussion instead.