What is wrong with my predicate function?

I am trying to use the remove_if method on std :: list. I want to remove the "special" element. Here is the code:

Class A {
public: 
void foo(size_t id) {
tasks.remove_if(&A::IsEqual(id)); //Here I have an error
}

private:
std::list<Task> tasks;
struct IsEqual {
    IsEqual(const Task& value) : _value(value) {}
    bool operator() (const size_t id) {
        return (_value._id == id);
    }
    Task _value;
    };
};

Can someone explain where the error is?

+4
source share
2 answers

Yours operator()must take an argument Task, since this is the type of element in tasks.

Another way to write:

tasks.remove_if([id](const Task& t) { return t._id == id });
+3
source

You have the wrong functor. The constructor must compare the value, and the operator () must complete the task:

struct IsEqual {

   IsEqual(const size_t id) : id(id) {}

   bool operator() (const Task& value) {
       return (value._id == id);
   }

   size_t id;
};
+3
source

Source: https://habr.com/ru/post/1585100/


All Articles