写了这样一段测试代码:
struct A
{
int f(int) { return 0;}
};
A a;
typedef function<int (int) > FunType;
FunType fun1 = bind(A::f, &a, _1);
FunType fun2 = bind(A::f, &a, _1);
bool result = fun1 == fun2; <<-----------编译失败
我在vc7.1下编译失败,错误信息如下:
error C2666: “boost::operator`=='” : 4 个重载有相似的转换。
这个错误实在有些莫名其妙,查了一下这四个可行的转换,一个是void boost::operator ==<R,T0,Allocator>(const boost::function1<R,T0,Allocator> &,const boost::function1<R,T0,Allocator> &),按道理,这个应该是最接近的,但是返回怎么是void类型?注释写的很清楚:就是为了毒害两个function比较的。:(
只好去看其他的比较实现了:
template<typename Functor>
BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
operator==(const function_base& f, Functor g)
还有左右交换一个。只支持Functor,不支持function_base作为参数,彻底晕了,想简单比较是没门的。
再看看,为什么不能简单比较?看源代码:
if (const Functor* fp = f.template target<Functor>())
return function_equal(g, *fp);
else return false;
哦,要functor就是为了target的时候获得类型啊!由于内部存储functor的时候,实际上是用的指针:
union any_pointer
{
void* obj_ptr;
const void* const_obj_ptr;
void (*func_ptr)();
char data[1];
};
比较指针肯定是不等的。所以,指望直接判等看来是不容易实现的。
struct A
{
int f(int) { return 0;}
};
A a;
typedef function<int (int) > FunType;
FunType fun1 = bind(A::f, &a, _1);
FunType fun2 = bind(A::f, &a, _1);
bool result = fun1 == fun2; <<-----------编译失败
我在vc7.1下编译失败,错误信息如下:
error C2666: “boost::operator`=='” : 4 个重载有相似的转换。
这个错误实在有些莫名其妙,查了一下这四个可行的转换,一个是void boost::operator ==<R,T0,Allocator>(const boost::function1<R,T0,Allocator> &,const boost::function1<R,T0,Allocator> &),按道理,这个应该是最接近的,但是返回怎么是void类型?注释写的很清楚:就是为了毒害两个function比较的。:(
只好去看其他的比较实现了:
template<typename Functor>
BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
operator==(const function_base& f, Functor g)
还有左右交换一个。只支持Functor,不支持function_base作为参数,彻底晕了,想简单比较是没门的。
再看看,为什么不能简单比较?看源代码:
if (const Functor* fp = f.template target<Functor>())
return function_equal(g, *fp);
else return false;
哦,要functor就是为了target的时候获得类型啊!由于内部存储functor的时候,实际上是用的指针:
union any_pointer
{
void* obj_ptr;
const void* const_obj_ptr;
void (*func_ptr)();
char data[1];
};
比较指针肯定是不等的。所以,指望直接判等看来是不容易实现的。