Boost logo

Boost Users :

From: Douglas Gregor (dgregor_at_[hidden])
Date: 2005-02-07 00:11:08


On Feb 6, 2005, at 12:38 PM, Tony Juricic wrote:

> ..or am I having a day of profound stupidity.
>
> documentation:
> Boost.Function
> 3.5 Comparing Boost.Function objects
>
> me: okay, boost function object wrapper objects are comparable.

Well, almost. You can compare a Boost.Function object against anything
it can possibly target, e.g., a function object. Perhaps this section
needs a different title.

> documentation:
> Chapter 5 FAQ
> 5.1 Comparison between boost::function objects cannot be implemented
> well, and therefore will not be implemented.
>
> me:okay so it is not implemented
> documentation:
> 5.1 This essentially negates the usefulness of operator==in the
> context in which it is most desired:multitarget callbacks.
>
> me: great, multitarget callbacks is exactly why I am reading all this
> documentation
>
> documentation:

Hmmm, this shouldn't definitely clarify that we have some support for
operator==, just not exactly what most people expect.

> 5.1 The Signals library has a way around this.
>
> me: great, if I only had an example of that way that wouldn't contain
> 2 free functions declared in the same file as the signal itself.
>
> Any help in clearing up my confusion greatly appreciated

The signals library returns a "connection" object that represents the
signal-slot connection. So, you can't just name a function object again
to remove it, you need to hold on to the connection object.

There is an alternative to using connection objects: make your
"disconnect" function a template. For instance, say we have:

   std::list<function<void(void)> > targets;

We can write a disconnect function like this:

   template<typename F>
   void disconnect(F f)
   {
     std::list<function<void(void)> >::iterator i =
std::find(targets.begin(), targets.end(), f);
     if (i != targets.end()) targets.erase(i);
   }

But, don't try to turn that template into a non-template taking a
function<void(void)>: it won't compile!

        Doug


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