Search code examples
visual-c++mfcpredicatec++98stdlist

std::list<int> predicate call to function: Error C3867 function call missing argument list


I am using std::list's predicate to update the list based on predicate. But calling in the OnInitDialog() throws compilation error. My code is as follows:

The below is .h:

class CDlgWindow : public CDialog
{
   private:
      bool single_digit (const int &value);
      int _days;
}

The below is .cpp:

    CDlgWindow::CDlgWindow(CWnd* pParent, CString strInfo, int days) //ctor
    { 
       _days = days;
      //_strInfo = strInfo
    }
    bool CDlgWindow::single_digit(const int& value)
    {
       return (value >= _days);
    }
    BOOL CDlgWindow::OnInitDialog()
    {
       CDialog::OnInitDialog();
       CenterWindow();
       .
       .
       .
       int numArr[] = {10,20,30,40};
       int size = sizeof(numArr)/sizeof(numArr[0]);
       std::list<int> numList (numArr, numArr+size);
       numList.remove_if(single_digit); //Error C3867 here!
       .
       .
    }

Complete error message: Error C3867 function call missing argument list, use '&CDlgWindow::single_digit' to create a pointer to member.

I am trying to understand the functors concept. As I checked in C++11, we have lambdas for easier implementation. Please guide me to understand more on this issue. Thanks!


Solution

  • std::list's remove_if member needs a unary predicate (p) that operates on values (v). The expression p(v) must be valid. Which it isn't if p is a non-static class member (see repro).

    There are two options:

    1. Make the predicate (single_digit) a static class member:
      class CDlgWindow : public CDialog
      {
         private:
            static bool single_digit (const int &value);
         // ...
      }
      
    2. Make the predicate a free function:
      bool single_digit(int const& value) {
          static int days_ = ...;
          return (value >= days_);
      }
      

    If you go with option 1 you will have to make _days static as well, since a static member function cannot access non-static instance data. If _days is a compile-time constant, make sure to mark it const as well. That'll open up some compiler optimizations.

    This is all hoping that things haven't significantly changed between C++98 and C++11. It's hard to find a C++98 compiler to verify this.