Search code examples
c++liststlstdlist

Using std::list::remove_if with MyClass' function


I have std::list<MyClass> *info1 I want to use (*info1).remove_if with a bool function of MyClass. As I see, everyone creating external functions or structs for remove_if. Isn't it possible to use it with bool MyClass::Condition() function?

Like this:

 class MyClass
{
private:
    int value;
public:
    bool Condition()
    {
      return ( value > 7500);
    }
};

And Then

   (*info1).remove_if(Condition())`

Solution

  • It is possible via std::mem_fn:

    info1->remove_if(std::mem_fn(&MyClass::Condition));
    

    Live demo

    or via std::bind:

    using namespace std::placeholders;
    info1->remove_if(std::bind(&MyClass::Condition, _1));
    

    Live demo

    Both of those functions are included in the standard <functional> header.