I'm trying to write predicate function for use with STL algorithms. I see that they are two ways to define a predicate:
(1) Use a simple function as below:
bool isEven(unsigned int i)
{ return (i%2 == 0); }
std::find_if(itBegin, itEnd, isEven);
(2) Use the operator() function as below:
class checker {
public:
bool operator()(unsigned int i)
{ return (i%2 == 0); }
};
std::find_if(itBegin, itEnd, checker);
I have more use for the second type as I usually would like to create a predicate object with some members in it and use them in the algorithm. When I add the same isEven function inside checker and use it as a predicate, I get an error:
3. Syntax which gives error:
class checker {
public:
bool isEven(unsigned int i)
{ return (i%2 == 0); }
};
checker c;
std::find_if(itBegin, itEnd, c.isEven);
Calling c.isEven gives an error during compilation saying undefined reference to some function. Can someone explain why 3. is giving error? Also, I would appreciate any pointers to read about predicate and iterator basics.
The example given says you should use the call operator (
operator()
) whereas in your example you've called your functionisEven
. Try re-writing it as:A pointer to a member function requires an instance to be called on, and you are passing only the member function pointer to
std::find_if
(actually your syntax is incorrect, so it doesn't work at all; the correct syntax isstd::find_if(itBegin, itEnd, &checker::isEven)
which then still doesn't work for the reasons I gave).The
find_if
function expects to be able to call the function using a single parameter (the object to test), but it actually needs two to call a member function: the instancethis
pointer and the object to compare.Overloading
operator()
allows you to pass both the instance and the function object at the same time, because they're now the same thing. With a member function pointer you must pass two pieces of information to a function that expects only one.There is a way to do this using
std::bind
(which requires the<functional>
header):If your compiler doesn't support
std::bind
, you can also useboost::bind
for this. Though there's no real advantage to doing this over just overloadingoperator()
.To elaborate a bit more,
std::find_if
expects a function pointer matching the signaturebool (*pred)(unsigned int)
or something that behaves that way. It doesn't actually need to be a function pointer, because the type of the predicate is bound by the template. Anything that behaves like abool (*pred)(unsigned int)
is acceptable, which is why functors work: they can be called with a single parameter and return abool
.As others have pointed out, the type of
checker::isEven
isbool (checker::*pred)(unsigned int)
which doesn't behave like the original function pointer, because it needs an instance ofchecker
to be called on.A pointer to a member function can be conceptually considered as a regular function pointer that takes an additional argument, the
this
pointer (e.g.bool (*pred)(checker*, unsigned int)
). You can actually generate a wrapper that can be called that way usingstd::mem_fn(&checker::isEven)
(also from<functional>
). That still doesn't help you, because now you have a function object that must be called with two parameters rather than only one, whichstd::find_if
still doesn't like.Using
std::bind
treats the pointer to a member function as if it was a function taking thethis
pointer as its first argument. The arguments passed tostd::bind
specify that the first argument should always be&c
, and the second argument should bind to the first argument of the newly returned function object. This function object is a wrapper that can be called with one argument, and can therefore be used withstd::find_if
.Although the return type of
std::bind
is unspecified, you can convert it to astd::function<bool(unsigned int)>
(in this particular case) if you need to refer to the bound function object explicitly rather than passing it straight to another function like I did in my example.I guess it's because the type of
c.isEven()
is,which may not be expected by
find_if()
.std::find_if
should be expecting either a function pointer (bool (*)(unsigned int)
) or a function object.Edit: Another constraint: A non-
static
member function pointer must be called by theclass
object. In your case, even if you succeed to pass the member function then stillfind_if()
will not have any information about anychecker
object; so it doesn't make sense to havefind_if()
overloaded for accepting a member function pointer argument.Note: In general
c.isEven
is not the right way to pass member function pointer; it should be passed as,&checker::isEven
.I prefer functors (function objects) because make your program more readable and, more importantly, expressing the intent clearly.
This is my favorite example:
Note: In recent years we've got a lambda expression support.
checker::isEven
is not a function; it is a member function. And you cannot call a non-static member function without a reference to achecker
object. So you can't just use a member function in any old place that you could pass a function pointer. Member pointers have special syntax that requires more than just()
to call.That's why functors use
operator()
; this makes the object callable without having to use a member function pointer.