Reputation: 7961
How can I pass member function pointer to std::function
through a function. I am going to explain it by comparison (Live Test):
template<class R, class... FArgs, class... Args>
void easy_bind(std::function<R(FArgs...)> f, Args&&... args){
}
int main() {
fx::easy_bind(&Class::test_function, new Class);
return 0;
}
I get an error message:
no matching function for call to ‘easy_bind(void (Class::*)(int, float, std::string), Class*)’
I just don't understand why a function pointer cannot be passed to std::function
when its being passed through a function parameter. How can I pass that function? I am willing to change the easy_bind
function parameter from std::function
into a function pointer but I really don't know how.
EDIT: Question simplified.
EDIT: Thanks to @remyabel, I was able to get what I needed: http://ideone.com/FtkVBg
template <typename R, typename T, typename... FArgs, typename... Args>
auto easy_bind(R (T::*mf)(FArgs...), Args&&... args)
-> decltype(fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...)) {
return fx::easy_bind(std::function<R(T*,FArgs...)>(mf), args...);
}
Upvotes: 6
Views: 9419
Reputation: 1637
http://en.cppreference.com/w/cpp/utility/functional/mem_fn is what you are supposed to use
struct Mem
{
void MemFn() {}
};
std::function<void(Mem*)> m = std::mem_fn(&Mem::MemFn);
Upvotes: 18
Reputation:
I think the problem can be narrowed down to this:
template<class R, class... FArgs>
void test(std::function<R(FArgs...)> f)
{
}
int main() {
test(&SomeStruct::function);
}
The error message is pretty similar without the rest of the easy_bind
stuff:
main.cpp: In function 'int main()':
main.cpp:63:31: error: no matching function for call to
'test(void (SomeStruct::*)(int, float, std::string))'
test(&SomeStruct::function);
main.cpp:63:31: note: candidate is:
main.cpp:49:10: note: template<class R, class ... FArgs>
void test(std::function<_Res(_ArgTypes ...)>)
void test(std::function<R(FArgs...)> f)
^
main.cpp:49:10: note: template argument deduction/substitution failed:
main.cpp:63:31: note: 'void (SomeStruct::*)(int, float, std::string)
{aka void (SomeStruct::*)(int, float, std::basic_string<char>)}'
is not derived from 'std::function<_Res(_ArgTypes ...)>'
test(&SomeStruct::function);
Essentially, it can't magically create an std::function
for you. You need something like your Functor
alias.
So thanks to the answer provided in generic member function pointer as a template parameter, here's what you can do:
//Test Case:
struct SomeStruct {
public:
int function(int x, float y, std::string str) {
std::cout << x << " " << y << " " << str << std::endl;
return 42;
}
};
template <typename Ret, typename Struct, typename ...Args>
std::function<Ret (Struct*,Args...)> proxycall(Ret (Struct::*mf)(Args...))
{
return std::function<Ret (Struct*,Args...)>(mf);
}
int main() {
auto func3 = fx::easy_bind(proxycall(&SomeStruct::function), new SomeStruct);
int ret = func3(5, 2.5, "Test3");
std::cout << ret << "\n";
return 0;
}
Now it works automatically.
Upvotes: 3