Correct Way to Define a Predicate Function in C++

前端 未结 5 980
不思量自难忘°
不思量自难忘° 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条回答
  •  死守一世寂寞
    2020-12-24 07:52

    I guess it's because the type of c.isEven() is,

    bool (checker::*)(unsigned int) // member function of class
    

    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 the class object. In your case, even if you succeed to pass the member function then still find_if() will not have any information about any checker object; so it doesn't make sense to have find_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.

提交回复
热议问题