1

Is it possible? The instruction bool b = (boost::bind(func, 1) == boost::bind(func, 1)) does not compile because it "cannot convert from 'boost::_bi::bind_t' to 'bool'". (The signature of func is void func(int).)

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
  • Why would you compare function pointers or anything similar??? – BЈовић Jul 30 '11 at 19:58
  • ... 'Cause I need to... Should I take that as a "no, it's not possible"? – Paul Manta Jul 30 '11 at 20:00
  • No, I have no idea, but I don't think that the return type of boost::bind has operator== defined – BЈовић Jul 30 '11 at 20:05
  • 1
    Are you sure you need to? Maybe you're approaching your problem the wrong way – Pablo Jul 30 '11 at 20:10
  • @Pablo Yes, I'm sure. I've been thinking about my particular problem for quite a few days now, and I finally found a very good solution... except it won't work unless you can somehow compare boost.bind. There's one other way I can fix my problem, but that would make the API slightly more complicated to use, so I'd rather not use it unless I'm certain there's no other way. – Paul Manta Jul 30 '11 at 20:18
  • 1
    See also ["Why is std::function not equality comparable?"](http://stackoverflow.com/questions/3629835/why-is-stdfunction-not-equality-comparable) The answers to that question also answer this question. – James McNellis Jul 30 '11 at 21:04

2 Answers2

3

Boost.Bind overloads the relational operators to return nested bind expressions. Thus in your code boost::bind(func, 1) == boost::bind(func, 1) returns a nullary (since there is no placeholders in your bind expressions) functor that, when called, returns func(1) == func(1). This is a convenient feature for predicates, amongst other uses:

typeded std::pair<T, U> pair_type;
// find pair where the first element is equal to 3
std::find_if(begin, end, boost::bind(&pair_type::first, _1) == 3);

In addition, the returned object is not convertible to bool and this is why it won't compile (disregarding the issue that it doesn't do what you want).

What you want to do is not part of the Boost.Bind interface. Tt would not be a very useful feature and in the (very) general case is undecidable.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
1

Don't know if this is "oficially supported functionality" but bind_t does seem to provide a function_equal method: http://www.boost.org/doc/libs/1_47_0/boost/bind/bind.hpp

Pablo
  • 8,644
  • 2
  • 39
  • 29
  • Hey, that seems very promising, thanks a lot. I'll test this tomorrow; I'm so tired right now. :) – Paul Manta Jul 30 '11 at 20:51
  • Thank you, Pablo, it works. As an alternative you can also use the compare member function: `boost::bind(func, 1).compare(boost::bind(func, 1))`. – Paul Manta Jul 31 '11 at 05:51