C++ 比较使用std::bind创建的std::函数

C++ 比较使用std::bind创建的std::函数,c++,c++11,std-function,stdbind,C++,C++11,Std Function,Stdbind,这里是一个玩具的例子 #include <iostream> #include <functional> struct Obj { int x; int foo() { return 42; } }; int main() { Obj a, b; std::function<int()> f1 = std::bind(&Obj::foo, &a); std::function<int()>

这里是一个玩具的例子

#include <iostream>
#include <functional>

struct Obj
{
  int x;

  int foo()
  {
    return 42;
  }
};

int main()
{
  Obj a, b;
  std::function<int()> f1 = std::bind(&Obj::foo, &a);
  std::function<int()> f2 = std::bind(&Obj::foo, &b);
  std::function<int()> f3 = std::bind(&Obj::foo, &a);
  std::function<int()> f4 = std::bind(&Obj::foo, &b);
}
#包括
#包括
结构对象
{
int x;
int foo()
{
返回42;
}
};
int main()
{
Obj a,b;
std::function f1=std::bind(&Obj::foo,&a);
std::function f2=std::bind(&Obj::foo,&b);
std::function f3=std::bind(&Obj::foo,&a);
std::function f4=std::bind(&Obj::foo,&b);
}
我如何验证
f1==f3
f2==f4
,这里的
==
比较器意味着两个
std::function
对象都对应于同一对象的相同方法?

您没有

如果您想要
==
您必须编写自己的类型擦除包装,可能需要使用
std::function
来存储状态

由于
std::bind
不支持
=
,lamba也不支持,因此必须插入一个支持
=
的自定义函数对象才能进行类型擦除

这些都不容易。其他解决方案,如使用名称注册绑定,将更加实用