views:

87

answers:

2

Hi, I have a class which contains boost::function as one of its arguments. I have to make this class equality comparable but the boost::function is not equality comparable. Is there a easy workaround for this problem?

Thanks, Gokul.

A: 

I tried it out, but it does not work as there is no operator to typecast to int* provided by boost::function. The following code refuses to compile:

void foo() {}

int main()
{
  boost::function<void()> f;
  f = foo;

  cout << (int*)f << endl;
}

I get the following error:
error: invalid cast from type ‘boost::function’ to type ‘int*’

I use gcc 4.1.2

Venkatesan
+1  A: 

boost::function is not eq_compare because there is good way to handle the fact that many functors are not eq_compare. Here is a bit of insight into it: http://www.boost.org/doc/libs/1_35_0/doc/html/function/faq.html#id690470

Unfortunately, the boosties decided not to provide a policy-based approach which would allow us to select the alternative, i.e. "eq-comparable functors only or bust" implementation, leaving us a bit stuffed here. There might be a couple of crappy workarounds for this situation but I'd suggest to either:

  1. ditch boost::function altogether and roll your own if you really,really need this eq_comparable thing. or
  2. See if your problem can be solved in a very different way. For example many people use function<> to implement a kind of event system. If that's the case, then you should have a look at boost::signals.
Luther Blissett
I never actually understood why, regular function pointers are comparable, so why can't you compare boost function, which to me seems like a wrapper for function pointers?
Viktor Sehr