mercredi 11 octobre 2017

Bind class method and pass it as function pointer

I would like to pass my class method as an argument to a (third-party) function that accepts a function pointer. Following is an example:

#include <functional>

typedef void(*pfnc) (void*);

struct Foo
{
    static void static_foo(void*)
    {
    }

    void foo(void*)
    {
    }

    void listner(pfnc f)
    {
    }

    void test()
    {
        listner(static_foo); // works with static method

        auto f = [](void*) {};
        listner(f); // works with lambda

        std::function<void(void*)> stdf = std::bind(&Foo::foo, this, std::placeholders::_1);
        listner(stdf);  // does not compile with not static method
    }
};

Unfortunately my solution does not compile. What do I have to change?

Aucun commentaire:

Enregistrer un commentaire