3

The following code won't compile because of "error: no matching function for call to β€˜mem_fun_ref()’" (gcc version 4.4.6).

#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>

using namespace std;

class toto
{
  char v[10];

public:
  toto(char* t) { memcpy(v, t, 9); }
  bool test(const char* var) const { return !strncmp(var, v, 9); }
  bool test(const string& var) const { return test(var.c_str()); }
};

int main()
{
  vector<toto> t;
  t.push_back("1");
  t.push_back("2");

  string name("2");
  vector<toto>::iterator it = remove_if(t.begin(), t.end(),
       bind2nd(mem_fun_ref(&toto::test), name)); // <= error
  t.erase(it, t.end());
  return 0;
}

I found a workaround: creating a

bool testZ(const string& var) const { return testZ(var); }

But I can't seem to find the correct template parameters, if that's even possible, to give to mem_fun_ref (or bind2nd?) to make it compile without my workaround.

Is there anyway to achieve this without my workaround, or is the workaround the "preferred" method?

BlakBat
  • 1,835
  • 5
  • 17
  • 21
  • Did you see [boost::bind](http://www.boost.org/doc/libs/1_55_0/libs/bind/bind.html) ? – P0W Feb 11 '14 at 18:20
  • We're not allowed to use boost on our projects for maximum compatibility with esoteric platforms. – BlakBat Feb 12 '14 at 10:48

1 Answers1

3

You should be able to cast it according to C++ overloaded method pointer:

bind2nd(mem_fun_ref((bool (toto::*)(const string&) const) &toto::test), name));