如何传递成员函数指针到std :: function [英] how to pass member function pointer to std::function
问题描述
如何通过函数传递成员函数指针到 std :: function
。我将通过比较解释(实时测试):
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;
}
我收到错误讯息:
no matching function for call to ‘easy_bind(void (Class::*)(int, float, std::string), Class*)’
我不明白为什么函数指针不能传递给 std :: function
当它通过一个函数参数。如何传递该函数?我愿意将
easy_bind
函数参数从 std :: function
更改为函数指针,但我真的不知道如何。
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.
编辑:问题简化。
编辑:感谢@remyabel,我需要: http://ideone.com/FtkVBg
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...);
}
推荐答案
缩小到此:
template<class R, class... FArgs>
void test(std::function<R(FArgs...)> f)
{
}
int main() {
test(&SomeStruct::function);
}
错误消息非常类似, > easy_bind stuff:
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);
本质上,它不能神奇地创建一个 std :: function
给你。您需要类似
Functor
别名。
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;
}
现在可以自动工作。
这篇关于如何传递成员函数指针到std :: function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!