Boost logo

Boost :

From: Andreas Huber (andreas_at_[hidden])
Date: 2001-11-21 17:31:50

Hello Doug,

Thanks a lot for your answer.

> I think that the "correct" way to write a publisher/subscriber
> mechanism is either what Peter proposed (by explicitly naming
> each subscriber) or by returning a token of some sort to use in
> removal:

Ok, I guess I was a bit too busy thinking that a general purpose
functor like boost::function should mimick a plain vanilla function
pointer as closely as possible ...

However, my claim is that equality operations would simplify the
usage since most C++ programmers know what a function pointer does
and how to deal with it. As with C# delegates (I'm no Microsoft fan
either, but I like the idea nevertheless), you could even pack the
whole publish-subscribe functionality into the functor itself.
Translated to boost::function you could write the following:

void fun1();
void fun2();

int main()
  boost::function< void > f1 = &fun1;
  boost::function< void > f2 = &fun2;

  // chaining operations +=, +, - and -= are only
  // supported for the void return type
  boost::function< void > f3 = f1 + f2;
  f3(); // this calls fun1 AND fun2
  f3 -= f2; // -= uses operator== in the implementation
  f3(); // this calls fun1 ONLY

Granted, functors referencing only one function (probably the
majority in most applications) carry around all the baggage needed to
reference an arbitrary number of functions, so this approach is not
very "C++-like" (you only pay for what you use). But there is a huge
number of C++ applications, where neither performance nor memory
footprint is an issue and implementation speed is _everything_. I'm
currently working in such a project and we are using functors for
publish-subscribe and asynchronous callbacks. I'm glad having chosen
to implement functors which support equality and chaining. Thanks to
the many similarities with native function pointers the functors were
easily introduced and immediately used by all developers, with very
few questions asked (nobody ever asked about adapting functors like
std::less or even custom functors).

However, I do see now that providing equality operations for
boost::function would do more bad than good mainly because it's not
difficult to live without them.

Thanks for enlightening me


Boost list run by bdawes at, gregod at, cpdaniel at, john at