boost 泛函数比较问题

adintr 2006-07-15 11:07:34
VC2005, boost1.33.1

代码:
#include <boost/function.hpp>

typedef boost::function<void (int)> bf_t;

int main()
{
bf_t first = 0;
bf_t second = 0;
first == second;
}

错误:
d:\workhome\testproj\thoo\thoo\thoo.cpp(43) : error C2666: 'operator ==' : 4 overloads have similar conversions
d:\workhome\thirdparty\include\boost\function\function_template.hpp(583): could be 'void boost::operator ==<R,T0,Allocator>(const boost::function1<R,T0,Allocator> &,const boost::function1<R,T0,Allocator> &)' [found using argument-dependent lookup]
with
[
R=void,
T0=int,
Allocator=std::allocator<void>
]
d:\workhome\thirdparty\include\boost\function\function_base.hpp(609): or 'bool boost::operator ==<bf_t>(Functor,const boost::function_base &)' [found using argument-dependent lookup]
with
[
Functor=bf_t
]
d:\workhome\thirdparty\include\boost\function\function_base.hpp(600): or 'bool boost::operator ==<bf_t>(const boost::function_base &,Functor)' [found using argument-dependent lookup]
with
[
Functor=bf_t
]
or 'built-in C++ operator==(void (__thiscall boost::function1<R,T0,Allocator>::dummy::* )(void), void (__thiscall boost::function1<R,T0,Allocator>::dummy::* )(void))'
with
[
R=void,
T0=int,
Allocator=std::allocator<void>
]
while trying to match the argument list '(bf_t, bf_t)'


难道 boost 的 function 是不能比较的?
...全文
606 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
adintr 2006-07-18
  • 打赏
  • 举报
回复
如果是类成员函数,里面除了保存函数指针外,还得保存对象指针吧,
如果只比较了函数指针,不比较对象指针那结果是错误的啊。
jixingzhong 2006-07-17
  • 打赏
  • 举报
回复
BOOST 库是个好冬冬啊 ...
看看先 ...
adintr 2006-07-17
  • 打赏
  • 举报
回复
顶一下,继续求解
lyskyly 2006-07-17
  • 打赏
  • 举报
回复
#include <boost/function.hpp>
#include<iostream>

using namespace std;

typedef boost::function<void (int)> bf_t;

bool operator == (bf_t fl, bf_t fr)
{
typedef void (*fun)(int);
fun temp1 = (fun)(fl.functor.obj_ptr);
fun temp2 = (fun)(fr.functor.obj_ptr);
return temp1 == temp2;
}
class A
{
public:
void operator()(int)
{
cout<<"operator()"<<endl;
}
static void foo(int)
{
cout<<"foo"<<endl;
}
};
void Fun(int)
{
}
int main()
{
bf_t first = &A::foo;
bf_t second = &A::foo;
first(1);
cout<<(first == second)<<endl;

first = boost::ref(A());
second = boost::ref(A());
first(1);
cout<<(first == second)<<endl;

}
对后两种情况也是可以的,不论一开始是什么形式,但它们转换为function后都是以一个指针保存的,所以只是比较这个指针的值是可以的
lyskyly 2006-07-17
  • 打赏
  • 举报
回复
呵呵,这里指考虑了一种情况
adintr 2006-07-17
  • 打赏
  • 举报
回复
这样比较也行?
boost::function<void (int)> 和 void (*fun)(int) 不一样啊,
前者可能是类的成员函数,也可能是一个重载了 operator() 的对象啊,这样比较行不行哦。
lyskyly 2006-07-17
  • 打赏
  • 举报
回复
如果要比较相不相等,这个重载或许可以,比较的是指针是否相等
#include <boost/function.hpp>
#include<iostream>

using namespace std;

typedef boost::function<void (int)> bf_t;

bool operator == (bf_t fl, bf_t fr)
{
typedef void (*fun)(int);
fun temp1 = (fun)(fl.functor.obj_ptr);
fun temp2 = (fun)(fr.functor.obj_ptr);
return temp1 == temp2;
}
void Fun(int)
{
}
int main()
{
bf_t first = Fun;
bf_t second = Fun;
cout<<(first == second)<<endl;
}
adintr 2006-07-17
  • 打赏
  • 举报
回复
就一个普通的 vector/list 之类, 如果相等都不能比较放进去之后就不能 find, 不能 erase 其中某个函数。
我也没要求它能比较大小,相等都不能比较,也太。。。
lyskyly 2006-07-17
  • 打赏
  • 举报
回复
还是可以放入非关联容器中去
而且个人觉得可以比较也没有什么意义,难道真的比较是指针相等,还是比较类型相等
如果实在要比较,可以自己重载比较操作符
adintr 2006-07-17
  • 打赏
  • 举报
回复
不能比较就不能放到 stl 的容器里面,这个 function 可用性大打折扣
lyskyly 2006-07-17
  • 打赏
  • 举报
回复
今天看到一篇BLOG,上面也讲到这个情况
写了这样一段测试代码:
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];
};
比较指针肯定是不等的。所以,指望直接判等看来是不容易实现的
双杯献酒 2006-07-17
  • 打赏
  • 举报
回复
GZ
adintr 2006-07-17
  • 打赏
  • 举报
回复
沉得真快,再顶顶
mdzhao 2006-07-15
  • 打赏
  • 举报
回复
mark
lyskyly 2006-07-15
  • 打赏
  • 举报
回复
不好意思,搞错了
lyskyly 2006-07-15
  • 打赏
  • 举报
回复
#include <boost\function.hpp>

typedef boost::function<void (int),std::allocator<void> > bf_t;

int main()
{
bf_t first = 0;
bf_t second = 0;
first = second;
}
lyskyly 2006-07-15
  • 打赏
  • 举报
回复
这是用了这里还有两个函数
operator==(const function_base& f, Functor g)
{
if (const Functor* fp = f.template target<Functor>())
return function_equal(*fp, g);
else return false;
}

template<typename Functor>
BOOST_FUNCTION_ENABLE_IF_NOT_INTEGRAL(Functor, bool)
operator==(Functor g, const function_base& f)
{
if (const Functor* fp = f.template target<Functor>())
return function_equal(g, *fp);
else return false;
}
lyskyly 2006-07-15
  • 打赏
  • 举报
回复
template<typename Functor>
inline bool operator==(const function_base& f, Functor g)
{
typedef mpl::bool_<(is_integral<Functor>::value)> integral;
return detail::function::compare_equal(f, g, 0, integral());
}

template<typename Functor>
inline bool operator==(Functor g, const function_base& f)
{
typedef mpl::bool_<(is_integral<Functor>::value)> integral;
return detail::function::compare_equal(f, g, 0, integral());
}
额是我看错了调用的是compare_equal(f, g, 0, integral());
lyskyly 2006-07-15
  • 打赏
  • 举报
回复
我想调用的是另一个operator==,但没有找到另一个operator==
template<typename F, typename G>
bool function_equal_impl(const F& f, const G& g, long)
{ return f == g; }

// function_equal_impl needs to be unqualified to pick
// user overloads on two-phase compilers

template<typename F, typename G>
bool function_equal(const F& f, const G& g)
{ return function_equal_impl(f, g, 0); }
adintr 2006-07-15
  • 打赏
  • 举报
回复
operator== 调用 function_equal, function_equal 又调用 operator==, 不死循环了?
加载更多回复(4)

64,641

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧