Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find_if within instance method

I have an instance method that populates a vector of strings. I am trying to find the one vector entry that contains a specific substring (for now, that substring is fixed - simple).

I have a .h:

namespace Data
{
namespace Shared
{

    class Logger
    {
      public:
        bool FindLogDirectoryPredicate(const string &str);
        int GetLogDirectory(string logConfigFile, string& logDirectory);
...
    }
}
}

and .cpp:

#include <algorithm>
#include <vector>
#include "Logger.h"

bool Logger::FindLogDirectoryPredicate(const string &str) 
{
    // Return false if string found.
    return str.find("File=") > 0 ? false : true;
}

int Logger::GetLogDirectory(string logConfigFile, string& logDirectory)
{
    vector<string> fileContents;
    ...
    vector<string>::iterator result = find_if(fileContents.begin(), fileContents.end(), FindLogDirectoryPredicate);
    ...
}

Compiling this in Visual Studio 2010, I receive:

Error   7   error C3867: 'Data::Shared::Logger::FindLogDirectoryPredicate': function call missing argument list; use '&Data::Shared::Logger::FindLogDirectoryPredicate' to create a pointer to member   Logger.cpp  317 1   Portability

Throwing an & in front of the function ref in the find_if call then results in:

Error   7   error C2276: '&' : illegal operation on bound member function expression    Logger.cpp  317 1   Portability

I did try to put the predicate function outside the class, but that didn't seem to work - gave me a function not found error. Tried qualifying the predicate with the class name... that gave me a different error in algorithm (header):

Error   1   error C2064: term does not evaluate to a function taking 1 arguments    c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm    83  1   Portability

The example I was following from here seems to indicate that this is relatively simple.... so what am I doing wrong?

like image 743
Jon Avatar asked Jun 09 '26 04:06

Jon


2 Answers

The problem is that FindLogDirectoryPredicate is an instance method: it's not enough to specify its name, you somehow have to specify which object that method should be called on. Now the answer to this question is obvious to us (this), but not to the compiler.

The classic way to do this is with

find_if(fileContents.begin(), 
        fileContents.end(), 
        bind1st(mem_fun(&Logger::FindLogDirectoryPredicate), this));

What's going on here?

mem_fun "converts a member function to a function object". That is, it creates an instance of a type (what type exactly is unspecified, but we don't care) that exposes operator() (this is what we do care about!). This operator expects the first parameter to be a pointer to an instance of the type that defines the member function; here, that would be an instance of Logger.

bind1st then takes this function object that takes two parameters (first is the pointer to instance, second is the original const string & parameter) and returns a different function object that takes just one parameter (the const string &). The other parameter is fixed to the value of bind1st's second argument (this).

Alternatively, if you can make FindLogDirectoryPredicate static then there's no longer any need to specify which instance to call it on, so the problem will automatically go away.

like image 67
Jon Avatar answered Jun 11 '26 18:06

Jon


Make the predicate static

class Logger
{
  public:
    static bool FindLogDirectoryPredicate(const string &str);
}

Or perhaps, use a lambda.

result = std::find_if(begin(), end(), [&this] (const std::string& s) 
     { return FindLogDirectoryPredicate(s); } );

You can also use a std::mem_fun (and related <functional> stuff) if you must use C++98/C++03

result = std::find_if(begin(), end(), 
     std::bind1st(std::mem_fun(&Logger::FindLogDirectoryPredicate), this) );
like image 31
sehe Avatar answered Jun 11 '26 19:06

sehe