Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-03-10 07:41:11


Douglas Gregor wrote:
> On Tuesday 09 March 2004 05:26 pm, Brian McNamara wrote:
>> In my opinion,
>> lambda(x) { return f(x) == g(x) }.
>> is best. Whenever I see a placeholder, I expect the whole expression
>> (modulo operator precedence and "constant(foo)" issues) to get
>> lambda-ized, and thus I expect operator== to create a lambda
>> expression. (I dunno if this is the 'right' expectation to have,
>> given the way the libraries work now, but I think it is a
>> reasonable/simple 'ideal' expectation.)
>
> I'd think that this is the right expectation for Lambda, but not for
> FC++ or Bind. Not that I'm helping any :)

No, I don't think so. Expressions that are valid for both Bind and Lambda
should have the same semantics. And in fact, this is not just theory:

#include <boost/bind.hpp>

namespace boost
{

namespace _bi
{

struct equal
{
    template<class V, class W> bool operator()(V const & v, W const & w)
const { return v == w; }
};

template<class R, class F, class L, class A2>
    bind_t< bool, equal, list2< bind_t<R, F, L>, typename
add_value<A2>::type > >
    operator== (bind_t<R, F, L> const & f, A2 a2)
{
    typedef list2< bind_t<R, F, L>, typename add_value<A2>::type >
list_type;
    return bind_t<bool, equal, list_type> ( equal(), list_type(f, a2) );
}

} // namespace _bi

} // namespace boost

#include <iostream>

int f(int x)
{
    return x + x;
}

long g(int x)
{
    return 2 * x;
}

int main()
{
    int x = 4;
    std::cout << ( boost::bind(f, _1) == 8 )(x) << std::endl;
    std::cout << ( boost::bind(f, _1) == boost::bind(g, _1) )(x) <<
std::endl;
}

>> More generally, I think it's not a good idea to try to create
>> "function equality" with operator==. I didn't participate in any of
>> the earlier discussions, but I think that if you want to
>> register/unregister function objects with an event handler, for
>> instance, you should use a separate "handle" object to keep track of
>> "function object identity".
>
> The problem is that there are a whole lot of use cases for equality of
> function objects, and people really want them. The especially want
>
> delegate<void()> f;
> f += some_function_object; // connect
> f -= some_function_object; // disconnect

There is no mention of operator== anywhere in the above, though.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk