THE PROBLEM
You cannot directly bind a member-function pointer belonging to type Foo
to std::function<void(int)>
, specifically because calling a non-static member-function requires an instance of type Foo
.
Foo obj; obj.member_function (); // can't call `member_function` without `obj`
Note: You can however bind &Foo::print_add
to std::function<void(Foo&, int)> x;
, and call it as x(instance_of_Foo, arg);
.
Note: It's also possible to bind it to std::function<void(Foo*, int>
, which would require a Foo*
instead of an lvalue of type Foo
.
THE SOLUTION
Instead you can use std::bind
to bind an instance of Foo
to the member-function in question, such as in the below example:
int main(){
Foo foo;
test (std::bind (&Foo::print_add, foo, std::placeholders::_1));
return 0;
}
Above we bind an instance of Foo
named foo
to the member-function pointer &Foo::print_add
.
The usage of std::placeholders::_1
tells std::bind
that we'd like it to generate a function-object that is callable using one (1) argument.
With the above snippet you will have the behaviour that you are currently asking for; my_func(5)
will be equivalent of calling foo.print_add (5)
.
DOCUMENTATION