boost function ==

Dear all, We all waited for the operator==. Unfortunately it does not compile on VC++ 7.1. What do I wrong: namespace Tstb { int f0 (); int f1 (int a); int f2 (int a, int b); int f3 (int a, int b, int c); } void Test { boost::function<int ()> fc1 = &Tstb::f0; boost::function<int ()> fc2 = &Tstb::f0; boost::function<int ()> fc3 = boost::bind(&Tstb::f1, 2); bool b = false; b = (fc1 == fc2); b = (fc1 == fc3); b = (fc2 == fc3); } It gives: c:\Work\Test\Testmfc\Testboost\Testfunction.cpp(164) : error C2666: 'operator`=='' : 4 overloads have similar conversions c:\Work Sdk\Boost\boost\function\function_base.hpp(562): could be 'boost::enable_if_c<B,T>::type boost::operator ==<boost::function<Signature>>(const boost::function_base &,Functor)' [found using argument-dependent lookup] with [ B=true, T=bool, Signature=int (void), Functor=boost::function<int (void)> ] c:\Work Sdk\Boost\boost\function\function_template.hpp(594): or 'void boost::operator ==<R,Allocator>(const boost::function0<R,Allocator> &,const boost::function0<R,Allocator> &)' [found using argument-dependent lookup] with [ R=int, Allocator=std::allocator<void> ] c:\Work Sdk\Boost\boost\function\function_base.hpp(571): or 'boost::enable_if_c<B,T>::type boost::operator ==<boost::function<Signature>>(Functor,const boost::function_base &)' [found using argument-dependent lookup] with [ B=true, T=bool, Signature=int (void), Functor=boost::function<int (void)> ] or 'built-in C++ operator== (boost::function0<R,Allocator>::safe_bool, boost::function0<R,Allocator>::safe_bool)' with [ R=int, Allocator=std::allocator<void> ] and [ R=int, Allocator=std::allocator<void> ] while trying to match the argument list '(boost::function<Signature>, boost::function<Signature>)' with [ Signature=int (void) ] and [ Signature=int (void) ] etc. Wkr, me

gast128 wrote:
Dear all,
We all waited for the operator==. Unfortunately it does not compile on VC++ 7.1. What do I wrong:
namespace Tstb { int f0 (); int f1 (int a); int f2 (int a, int b); int f3 (int a, int b, int c); }
void Test { boost::function<int ()> fc1 = &Tstb::f0; boost::function<int ()> fc2 = &Tstb::f0; boost::function<int ()> fc3 = boost::bind(&Tstb::f1, 2);
bool b = false;
b = (fc1 == fc2); b = (fc1 == fc3); b = (fc2 == fc3); }
You've been mislead by the name. function::operator== is not an operator==. Here's how it's supposed to be used: boost::function<int ()> fc1 = &Tstb::f0; boost::function<int ()> fc3 = boost::bind(&Tstb::f1, 2); bool b = false; b = (fc1 == &Tstb::f0); b = (fc3 == &Tstb::f0); b = (fc1 == boost::bind(&Tstb::f1, 2)); b = (fc3 == boost::bind(&Tstb::f1, 2));
Wkr, me
:-(

Ok thank you. We use boost function also as a client defined callback. Because there is no operator==, it is very hard to unregister. So we came up with a solution probably comparable to the signals unregister solution. Wkr, me

On Dec 15, 2004, at 12:47 PM, gast128 wrote:
Ok thank you. We use boost function also as a client defined callback. Because there is no operator==, it is very hard to unregister. So we came up with a solution probably comparable to the signals unregister solution.
Are you sure that the provided operator== doesn't work? The motivation for this formulation of operator== (other than the unimplementability of the general case) was that it solved Herb Sutter's "delegate" problem. For instance, you can write this: template<typename Signature> class delegate { typedef boost::function<Signature> callback_type; std::list<callback_type> callbacks; public: template<typename F> void register(const F& f) { callbacks.push_back(f); } template<typename F> void unregister(const F& f) { typename std::list<callback_type>::iterator pos = std::find(callbacks.begin(), callbacks.end(), f); if (pos != callbacks.end()) callbacks.erase(pos); } }; Doug
participants (3)
-
Doug Gregor
-
gast128
-
Peter Dimov