Code:
#include<functional>
typedef void (*foo)(int);
typedef std::function<void(int)> bar;
class X
{
public:
void fun(int x){};
};
int main()
{
X x;
bar b=std::bind(&X::fun,&x,std::placeholders::_1); //This works
// foo f=std::bind(&X::fun,&x,std::placeholders::_1); //This doesn't
}
But I need the to pass the function to a posix function (signal) defined to take function pointers of type foo.
Is there a way I can pass a member function (along with the object) to something defined to take function pointers only?
EDIT:
There turns out to be a member function of std::function that appears to do what I need, but after successfully compiling, promptly segfaults.
Code:
#include<functional>
#include<iostream>
typedef void (*foo)(int);
typedef std::function<void(int)> bar;
class X
{
public:
X(int x):y(x){};
void fun(int x){std::cout<<x<<y;};
private:
int y;
};
int main()
{
X x(4);
bar b=std::bind(&X::fun,x,std::placeholders::_1);
foo f=*b.target<foo>();
f(5);
}