lundi 20 avril 2015

C++ function that take a member function and return a static lambda/std::function to it

I would like to implement a member function that take a pointer to an other member function and return a static binding. Could be whatever: a lambda or a ? std::function.

Since this moment, I made the following intent in C++11 without success:

#include <functional>
#include <iostream>

class MyClass{
public:
    auto link( std::function<void(MyClass::*)(int)> func)->std::function<void(int)>
    {
        return [this, func](int i){func(i);};
    }
    auto myFunc(int i)->void { std::cout << "Ok:" << i << std::endl; }

    auto test ()->void
    {
        link(&MyClass::myFunc)(4);
    }
};

auto main()->int
{
    MyClass c;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire