如何解决与静态成员函数一起使用时关于 std::function 的问题
为什么 PassFxn(&X::StaticmemberDoIt);
和 PassFxn(std::bind(&X::StaticmemberDoIt,_1,_2,_3));
都是正确的?调用 PassFxn(&X::StaticmemberDoIt);
时是否存在隐式转换,因为声明是 void PassFxn(std::function<int(float,std::string,std::string)> func)
而非 void PassFxn(int(*func)(float,std::string))
?
PassFxn(&X::StaticmemberDoIt);
和 PassFxn(X::StaticmemberDoIt);
之间有什么区别?
这是演示的代码片段(https://coliru.stacked-crooked.com/a/f8a0e1bb60550958):
#include <functional>
#include <string>
#include <iostream>
void PassFxn(std::function<int(float,std::string)> func)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
int result = func(12,"a","b"); // call using function object
std::cout << result << std::endl;
}
struct X
{
int MemberDoIt(float f,std::string s1,std::string s2)
{
std::cout << "Member: " << f << "," << s1 << "," << s2 << std::endl;
return 0;
}
static int StaticmemberDoIt(float f,std::string s2)
{
std::cout << "Static: " << f << "," << s2 << std::endl;
return 0;
}
};
int main()
{
using namespace std::placeholders;
X x;
PassFxn(std::bind(&X::MemberDoIt,x,_3)); // Use a member function!
// Or,if you have a *static* member function...
//Why these four expression are all right?
PassFxn(X::StaticmemberDoIt);
PassFxn(&X::StaticmemberDoIt);
PassFxn(std::bind(X::StaticmemberDoIt,_3));
PassFxn(std::bind(&X::StaticmemberDoIt,_3));
// ...and you can basically pass any callable object!
}
解决方法
对于常规函数(以及静态方法),从 C 继承的语法允许 f
和 &f
作为函数指针。
函数指针和 std::bind
的结果都是可调用的,因此是 std::function
的有效参数(并作为 std::bind
的第一个参数)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。