3

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?

kynan
  • 13,235
  • 6
  • 79
  • 81
Jon
  • 1,675
  • 26
  • 57

3 Answers3

4

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thank you Jon (and sehe and Andrey). I've accepted this answer as it offered an explanation as well as a solution. I've specified the predicate as static and the code now compiles. Now, on to testing. Thanks again... knew it had to be something relatively simple. – Jon Apr 23 '12 at 14:03
3

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) );
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I don't think mem_fun_ref would work. The best you can get out of that is calling member functions with no arguments. You'd need bind from tr1 or boost. – Nathan Monteleone Apr 23 '12 at 13:56
  • @NathanMonteleone of course, you'd need to bind argument, which the standard library has supported from C++98 no problem. Expanded my sample. Boost is nice too – sehe Apr 23 '12 at 14:02
  • What do you mean by "use a lambda"? Also, not sure what you are referring to with "C++98/C++03"? I gather they're version #'s of the language, but how does one determine what is used (and what is available to a given compiler)? – Jon Apr 23 '12 at 14:05
  • Recent compilers (gcc, clang, VC10/11) implement (a subset) of the latest C++11 spec which supports lambdas. My answer already shows what they look like. See also [C++11 support](http://stackoverflow.com/questions/5047971/detecting-c11-support) and [Lambdas](http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11) – sehe Apr 23 '12 at 14:31
  • @sehe Neat, I didn't realize mem_fun could peel off arguments like that. – Nathan Monteleone Apr 23 '12 at 15:11
  • Have done some reading on lambdas and those are really cool! I wound up writing one for our application as this is one of those "one-off" situations. Thanks! If I could set multiple answers as accepted, I would add this one as well! – Jon Apr 25 '12 at 15:04
-1

Make your predicate a static class member.

static bool FindLogDirectoryPredicate(const string &str);
Andriy
  • 8,486
  • 3
  • 27
  • 51