Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:58:29+00:00 2026-05-31T03:58:29+00:00

The snippet below works. However, it’s a bit ugly because it uses a static

  • 0

The snippet below works. However, it’s a bit ugly because it uses a static method to wrap a method call to a predicate.

In other words, I would like to replace:

c.remove_if_true( Value::IsOdd );   // static method

with something like

c.remove_if_true( Value::isOdd );   // member method

There should be one fewer level of indirection and hopefully, the resultant code would be clearer.

How do I refactor my code to call isOdd() directly without having to go through a static method wrapper?

However, if this implementation is as clear as I can make this code, also let me know. TIA.

#include <vector>
#include <functional>

template< typename T >
class MyContainer
{
public:
    typedef std::function<bool(const T& t)>   PREDICATE;

public:
    void remove_if_true( PREDICATE predicate )
    {
        // NOTE: use implementation from KennyTM's answer below
    }
private:
    std::vector< T >  m_vec;
};

class Value
{
public:
    Value( int i ) : m_i( i ) { }
    bool isOdd() const { return m_i%2==1; }
    static bool IsOdd( const Value& v ) { return v.isOdd(); }
private:
    int m_i;
};


int main()
{
    MyContainer<Value> c;

    c.remove_if_true( Value::IsOdd );  // would like to replace with Value::isOdd here
}

Solution using KennyTM’s Answer

ataylor’s suggestion std::mem_fun_ref() required with gcc 4.6.1 and other compilers not completely up-to-date with latest standards

#include <vector>
#include <algorithm>
#include <functional>

template< typename T >
class MyContainer
{
public:
    typedef std::const_mem_fun_ref_t<bool, T>  PREDICATE;

public:
    void remove_if( PREDICATE predicate )
    {
        auto old_end = m_vec.end();
        auto new_end = std::remove_if(m_vec.begin(), old_end, predicate);
        m_vec.erase(new_end, old_end);
    }
private:
    std::vector< T >  m_vec;
};

class Value
{
public:
    Value( int i ) : m_i( i ) { }
    bool isOdd() const { return m_i%2==1; }
private:
    int m_i;
};


int main()
{
    MyContainer<Value> c;

    c.remove_if( std::mem_fun_ref( &Value::isOdd ));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:58:30+00:00Added an answer on May 31, 2026 at 3:58 am
    c.remove_if_true( std::mem_fn(&Value::isOdd) );
    

    BTW, is there any reason you need to avoid std::remove_if?

    void remove_if_true(PREDICATE predicate)
    {
        auto old_end = m_vec.end();
        auto new_end = std::remove_if(m_vec.begin(), old_end, predicate);
        m_vec.erase(new_end, old_end);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This following code snippet works in FF, IE and Chrome. However It does not
The snippet below is generating weird output: for s in servers: vo = ss.getServerVO(s)
In the code snippet below, from my jQuery setup, I need to check if
In the code snippet below, I am trying to persist two entities - Account
//I have created below snippet to let the sensor to be detected. -(void)addProximitySensorControl {
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
I have below code snippet SimpleDateFormat dateFormat = new SimpleDateFormat( yyyy-MM-dd hh:mm:ss.SSS); String processedContentDate=2012-04-10
I have a script snippet looks like below: <ItemGroup> <files Include=*.txt></files> </ItemGroup> <Message Text=@(files)>
Please look at the below code snippet and let me know how the out
I have this snippet of code below. I want to pass the value of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.