Boost logo

Boost Users :

Subject: Re: [Boost-users] comparing function objects
From: Noah Roberts (roberts.noah_at_[hidden])
Date: 2008-09-24 18:43:10


Noah Roberts wrote:
> "Function object wrappers can be compared via == or != against any
> function object that can be stored within the wrapper. If the function
> object wrapper contains a function object of that type, it will be
> compared against the given function object (which must be either be
> EqualityComparable or have an overloaded boost::function_equal)."
>
> But how do I compare two boost::functions? This code doesn't work:
>
> #include <boost/function.hpp>
> #include <boost/bind.hpp>
>
> double f(double)
> {
> return 42.0;
> }
>
> int main()
> {
> boost::function<double(double)> f1 = boost::bind(&f, _1);
> boost::function<double(double)> f2 = boost::bind(&f, _1);
>
> //if (f1 == f2)
> if (boost::function_equal<boost::function<double(double)>,
> boost::function<double(double)> >(f1,f2))
> ;
> }
>
> NOTE: tried to specifically instantiate the function_equal template as
> attempt3. Previous attempt didn't do that...it also did not work.

Oh, and the errors from mingw:

g++ -Ie:\boost\include -O0 -g3 -Wall -c -fmessage-length=0 -omain.o
..\main.cpp
e:/boost/include/boost/function_equal.hpp: In function `bool
boost::function_equal_impl(const F&, const G&, long int) [with F =
boost::function<double ()(double)>, G = boost::function<double
()(double)>]':
e:/boost/include/boost/function_equal.hpp:24: instantiated from `bool
boost::function_equal(const F&, const G&) [with F =
boost::function<double ()(double)>, G = boost::function<double ()(double)>]'
..\main.cpp:15: instantiated from here
e:/boost/include/boost/function_equal.hpp:17: error: ambiguous overload
for 'operator==' in 'f == g'
e:/boost/include/boost/function_equal.hpp:17: note: candidates are:
operator==(void (boost::function1<double, double>::dummy::*)(), void
(boost::function1<double, double>::dummy::*)()) <built-in>
e:/boost/include/boost/function/function_base.hpp:739: note:
      typename boost::enable_if_c< boost::type_traits::ice_not<
boost::is_integral<T>::value>::value, bool>::type
boost::operator==(const boost::function_base&, Functor) [with Functor =
boost::function<double ()(double)>]
e:/boost/include/boost/function/function_base.hpp:748: note:
      typename boost::enable_if_c< boost::type_traits::ice_not<
boost::is_integral<T>::value>::value, bool>::type
boost::operator==(Functor, const boost::function_base&) [with Functor =
boost::function<double ()(double)>]
e:/boost/include/boost/function/function_template.hpp:826: note:
          void boost::operator==(const boost::function1<R, T0>&, const
boost::function1<R, T0>&) [with R = double, T0 = double]
Build error occurred, build is stopped
Time consumed: 781 ms.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net