dimanche 20 août 2017

C++ Functor template for class member functions

There is a small functor class I wrote which should be able to call class member functions by hilding a static wrapper functiona nd a void pointer to the object. The example below does not compile due to an error when setting the wrapper function. What I want is a class member pointer as template argument. Does anyone know what is wrong there?

I think there could be another problem in the static function when calling the member function. I do not exactly know how to do this with template syntax. The minimal example compiles with C++11 enabled gcc.

#include <iostream>

template<class TReturn, class... TParameter>
struct Functor {

    TReturn (*ptr)(void*, TParameter...);
    void     *object;

    template<class TObject, class TMemberFunction>
    static TReturn memberCaller(void *obj, TParameter... params) {
        TObject *c = static_cast<TObject*>(obj);
        return (c->*(TObject::TMemberFunction))(params...);
    }

    TReturn operator()(TParameter... params) {
        return ptr(object, params...);
    }
};

class Test {
public:
    void func(int a) {
        std::cout << a << std::endl;
    }
};

int main(int argc, const char **argv) {
    Functor<void, int> f;
    Test               t;

    f.object = &t;
    f.ptr    = &Functor<void, int>::memberCaller<Test, Test::func>;

    f(100);
}

Aucun commentaire:

Enregistrer un commentaire