|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2004-03-10 17:49:58
On Wednesday 10 March 2004 11:27 am, Brian McNamara wrote:
> I don't see why people can't cope with
>
> delegate<void()> f;
> handle h = f.connect( some_function_object );
> ...
> f.disconnect( h );
>
> instead. Indeed, it seems to me that it might be cheaper to hold onto
> the handle in some cases rather than the function object. (A handle
> could presumably be an int, whereas a function object may be many words
> of data or maybe even require heap storage.) But admittedly, I don't
> typically work in these domains to know how people _actually_ use (or
> want to use) these things.
Oh, I know, and I mostly agree. I tend to liken += and -= to new and delete,
whereas the handle idiom (they're calling "connections" in Signals) is like
having a smart pointer.
> Maybe the best compromise is to define a new entity for "function
> equality". Define a function (result_of-capable functor, really) like
>
> boost::function_equal( f, g )
>
> which has this behavior:
>
> - if f and g are both function pointers, use operator==, else
>
> - if one or both are user-defined types, call f.function_equal(g)
> (or g.function_equal(f)) if it exists, else
>
> - return false (or fail to compile, whichever people decide is best)
We can have boost::function_equal(f, g), but I'd prefer that it always use
operator==. Then boost::lambda, boost::bind, etc. can just overload
function_equal. Why? Because most users just want to write a normal
operator== and have it work; that's what makes sense. Just because Lambda can
do some screwy things with syntax (and believe me, I love Lambda) doesn't
mean others should have to use a weird comparison syntax.
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk