Correct Way to Define a Predicate Function in C++

前端 未结 5 977
不思量自难忘°
不思量自难忘° 2020-12-24 07:22

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:

5条回答
  •  梦毁少年i
    2020-12-24 07:59

    checker::isEven is not a function; it is a member function. And you cannot call a non-static member function without a reference to a checker 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.

提交回复
热议问题